1
votes

I try to kill a process with the kill command in linux. (not using -9 as argument)

I need to make sure that the process is really killed. As far as I know, the kill command runs asynchronously and it can take some time till it is finished.

I need to make sure, after I run the kill that my process has died using bash

Can you please assist?

Thanks!!!

3

3 Answers

4
votes

Killing a process with signal 0 will check if the process is still running, and not actually kill it. Just check the return code.

4
votes

Assuming $PID holds the pid of your process, you could do something like this:

kill "$PID"

while [ $(kill -0 "$PID") ]; do
  sleep 1
done

echo "Process is killed"
0
votes

kill is used to send signals to processes. It doesn't necessarily terminate the process (but usually do). kill without explicitly mentioned signal will send SIGTERM to the process. The default action on SIGTERM is to terminate process but process can setup a different signal handler and process might not be terminated.

What, I think you need, is a way to find if the process has handled the signal or not. This can be done using ps s $PID. If this shows 0s as pending mask, the process has received the signal and processed it.