So, I recently found out that kill is not a synchronous command, so I use this while loop in bash, which is awesome:
while kill PID_OF_THE_PROCESS 2>/dev/null; do sleep 1; done
However, there are cases (very rare, but they still occur) in which the process gets stuck and does not affect the kill signal. In these cases, the only way to kill the application is " kill -9 ".
So, I am wondering how to change the while loop above, in bash, to use the -9 argument only if the loop has reached the 10th iteration?
.... , ... ...
#!/bin/bash i=0 PID_OF_THE_PROCESS="your pid you can set as you like" # send it just once kill $PID_OF_THE_PROCESS 2>/dev/null; while [ $i -lt 10 ]; do # still alive? [ -d /proc/$PID_OF_THE_PROCESS ] || exit; sleep 1; i=$((i+1)) done # 10 iteration loop and still alive? be brutal kill -9 $PID_OF_THE_PROCESS
, , .
, , , , 0 , , , . (kill -0 $pid , .) , , kill -9 . , , , , . , .
0
kill -0 $pid
kill -9
, .
kill $PID 2>/dev/null sleep 10; if kill -0 $PID 2>/dev/null kill -9 $PID fi
:
c=0 while true; do echo $c; c=$((c+1)); if [ $c -eq 10 ]; then break; fi; done
, , :
count=0 while kill PID_OF_THE_PROCESS 2>/dev/null do sleep 1; (( count++ )) if (( count > 9 )) then kill -9 PID_OF_THE_PROCESS 2>/dev/null fi done