Search code examples
bashwatchmdadm

Kill watch from bash


I need to be able to call this:

watch -n1 cat /proc/mdstat

from bash.

For raid creating watching (after mdadm --create etc.), and then kill it then building process will end.

#!/bin/bash
#PID=$!
while
progress=$(cat /proc/mdstat |grep -oE 'recovery = ? [0-9]*')
do
    watch -n1 cat /proc/mdstat
    PID=$(pidof watch)
    echo "$PID" >> /mnt/pid
    if (("$progress" >= "100"))
        then
            break
            kill -9 $PID
    fi
done
echo "done" 

But I can not figure out how to kill watch out from bash. I tried PID=$! and PID=$$, pidof watch at the cycle and out of him, but can't assign correct PID to my variable to make kill -9 $PID.


Solution

  • It sounds like you'll need to have watch running until progress reaches 100 ?

    watch  -n1 cat /proc/mdstat &
    WATCHPID=$!
    while
    progress=$(cat /proc/mdstat |grep -oE 'recovery = ? [0-9]*')
    do
    
    if (("$progress" >= "100"))
        then
            break
    
    fi
    sleep 1
    done
    
    kill $WATCHPID
    
    echo "done"