Linux: how to detect death of a process?

7,297

You could parse /proc/%d/stat which if the PID currently exists will give you information about the process. This will work on any Linux system but not other unix systems.

To parse the file find the last ) character. It will be followed by a space and the current state of the process. Maybe you don't care about the state, maybe you do care if the state is Z which means the process is dead but the parent process has not yet collected the status.

19 fields further into the data you will find the start time of the process. If the PID has been recycled the start time has changed. Here is an example command line that will print just the start time:

cat /proc/2272/stat | sed -e 's/.*) //' | cut -f20 -d' '
Share:
7,297

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I would like to detect that a certain process in linux have died. Say that pid of that process is 1234. I can check that its pid is in use by testing the existence of /proc/1234/. Still, it is possible that the process have died and pid was reused. I can check if the inode of some file in proc (say proc/1234/cmdline) is the same and hope that a new process will get a different value.

    Is there any better way to reliably detect death of a process?