An easy way to detect if a process crashed (or stopped)?
I need to detect if a process stopped (doesn't matter if crashed, exited normally, or anything else).
My setup is like this : - task A - does some processing (and occasionally crashes).
- task B - starts the task A, and waits for resultsMy idea is to open a file in task A right at the beginning, and to use inotify in task B to detect when task A opened and closed the file. When task A stops (normally o开发者_如何学运维r crash), the file will be closed, and task B will detect this. (when task A crashes, the file will be closed, right?)
My questions are :
- Is the above going to work? - Is there a simpler or better way to detect task A stopping?PS If it matters, I am using fedora 9
EDIT
The setup above is very simplified. I have 2 of task B, and about 30 of task A. Therefore, I can not use system
and fork
It is much easier to use daemon monitoring tools like runit
or watch
. These are custom-made for this job, e.g. watching a controlled process run and die. If you do code this yourself, use wait
and friends to wait for a spawned process.
taskB should use fork()
to make taskA its child process. Then you can wait(&status)
(in C)... status
will hold the exit code of task A. You can read status
to check to see if it exited normally or with some sort of error code.
You could use a tcp socket - i.e. set B up as a server and allow A to connect. B should write a byte to A periodically (whatever delay you want before detecting that A has gone away). When the connection goes away (i.e. the write fails) you know A has gone away.
精彩评论