Reputation: 683
I am running a process , from which I would like to get notification of the termination of some another process. In windows we can use WaitForSingleObject, by passing the handle of the process whos termination we are intersted in . I am new to Linux world , please suggest some approach.
Upvotes: 3
Views: 1558
Reputation: 21956
In modern Linux (starting from kernel version 5.3), there's pidfd_open
kernel API which allows to wrap process ID into a handle compatible with poll
, epoll
, and the rest of them.
Upvotes: 0
Reputation: 6387
There are multiple Linux APIs that you can use to do this. Here are some of them:
There are really too many details to properly compare these options in a single StackOverflow answer, but I've written about this in detail on my blog.
Upvotes: 1
Reputation: 62459
On Linux (and indeed most *NIXen), you can only wait for processes that are children of the current process, unless you have root privileges (or effective capabilities that allow general tracing - this can be arranged through the capability system without granting full root access, but requires consciously configuring it...), where you can use ptrace()
to attach to arbitrary processes in order to monitor them.
Upvotes: 1
Reputation: 10570
If you can get the process id of the child process inside the child process, then you can save it into a file inside the child process and read it from there in the parent process. Then in the parent process you can poll the existence of the child process with ps ax | cut -b 1-5 | grep fooprocessid
at constant intervals. More elegant methods certainly exist, but this works in any programming language, in which in the child process you can get the process id and in the parent process you can execute commands.
If you know some details that can be found out with ps
(or top
) and that distinguish the child process from all other processes, then you don't need even the process id of the child process, ps ax | grep foo
is sufficient. Or ps
with some other paramaters, depending on what details you know about the child process.
Upvotes: 0