Ashish Mittal
Ashish Mittal

Reputation: 683

how to get notified on termination of some another process in Linux

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

Answers (5)

Soonts
Soonts

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.

Manual page.

Upvotes: 0

Natan Yellin
Natan Yellin

Reputation: 6387

There are multiple Linux APIs that you can use to do this. Here are some of them:

  • Netlink process connector - this an API to do precisely what you want but there are issues tracking detailed information short-lived processes
  • audit API - a kernel API enabled by default on most distributions which can send events to usermode for every syscall (if you go this route you need to track exec-like and fork-like syscalls)
  • tracepoints and kprobes - two kernel debugging APIs which can be used to get information on events about process lifecycle
  • ebpf based solutions - can be used in conjunction with tracepoints/kprobes to filter the events in kernel or run various logic
  • ptrace based solutions - these include simple ptrace debugging as well as the seccomp API

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

twalberg
twalberg

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

nrz
nrz

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

shx2
shx2

Reputation: 64328

It might be worth checking out supervisord

Upvotes: 1

Related Questions