Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out when process exits in Linux?

Tags:

linux

process

I can't find a good way to find out when a process exits in Linux. Does anyone have a solution for that?

One that I can think of is check process list periodically, but that is not instant and pretty expensive (have to loop over all processes each time).

Is there an interface for doing that on Linux? Something like waitpid, except something that can be used from unrelated processes?

Thanks, Boda Cydo

like image 859
bodacydo Avatar asked Sep 09 '10 12:09

bodacydo


People also ask

How do I see all running processes in Linux?

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.

How do you check if a process is killed in Linux?

To verify that the process has been killed, run the pidof command and you will not be able to view the PID. In the above example, the number 9 is the signal number for the SIGKILL signal.


2 Answers

You cannot wait for an unrelated process, just children.

A simpler polling method than checking the process list, if you have permission, you can use the kill(2) system call and "send" signal 0.

From the kill(2) man page:

If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID

like image 110
camh Avatar answered Oct 18 '22 02:10

camh


Perhaps you can start the program together with another program, the second one doing whatever it is you want to do when the first program stops, like sending a notification etc.

Consider this very simple example:

sleep 10; echo "finished"

sleep 10 is the first process, echo "finished" the second one (Though echo is usually a shell plugin, but I hope you get the point)

like image 35
Erich Kitzmueller Avatar answered Oct 18 '22 01:10

Erich Kitzmueller