How to check the existence of a process kill -0

To confirm the existence of the process, use the kill command and specify 0 as an option. Rest assured that if you specify the 0 option for kill, the process will not be terminated.

kill -0 process number

If the process exists, the command will succeed and nothing will be output. If the command fails, you will get a message that there is no such process.

This can be confirmed by examining $? . If it succeeds, it will be 0 , and if it fails, it will be 1 .

The following is a sample script.

kill -0 16033 > /dev/null 2>&1

if [ $? = 0 ]; then
  echo "exists";
else
  echo "not exists"
fi

Associated Information