UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2016-09-27 08:15 AM

malati
Member
Registered: 2016-09-27
Posts: 4

Process Monitoring

Hi All,
I am writing monitoring tool in c++ which job is to restart the processes if they goes down abnormally. Then it will take maximum information of process which went down
and send this information to interested recipients.

Most of our processes which needs such monitoring are always starting in shell script. Script job is to  set some environmental variables and then start process.

Monitoring tool takes shell script as argument then start that script using  fork and exec system call. This script then internally starts the processes in it.

For getting dieing process information i am making used of waitpid system call. But shell script becomes child process of monitoring process. But we actually intended to monitor
process which shell script is starting. After scripts dies its child process ppid becomes 1(init).

We wanted to monitor child process of script but not able to find appropriate solution for it.
Could any one help me to resolve this issue.

Thank you in advance for your help.

Thanks
Malati.

Offline

#2 2016-09-27 12:28 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,827
Website

Re: Process Monitoring

There's no standard way to effectively reparent orphaned grandchildren processes to yourself instead of letting them become children of init...  However, if you don't mind unportable code and you happen to be on Linux 3.4+, there is prctl(PR_SET_CHILD_SUBREAPER), which supposedly can do what you want...

The other option is to change your shell scripts so they exec into the processes they're starting...  Of course, if those processes do a fork() of their own to auto-background, you'll have the same problem...

Offline

#3 2016-09-27 01:04 PM

malati
Member
Registered: 2016-09-27
Posts: 4

Re: Process Monitoring

Thanks for your reply..its very useful but I don't have linux version 3.4+ on my pc..also I don't have authority to change script..

There is another way that if i am able to wait main process for processes other than its child processes..is there any way to do this ?

Offline

#4 2016-09-27 07:07 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,827
Website

Re: Process Monitoring

Not really, at least not in any standard way...  Other than becoming init yourself...

Well, you could ptrace() the process to become its pseudo-parent, I suppose...  But, that would be pretty ugly, I think...

But, if you're just monitoring processes and restarting them on death, all you really need to do is periodically look to see if the process is still running...  Just wake up every so often and look for the process...  You won't get instant notification on deaths, but as long as you don't sleep too long in between checks, it shouldn't be too bad...

Another Linux-specific solution might be netlink sockets...  I think one of those (maybe NETLINK_AUDIT) should send notifications of all process deaths you could probably listen for...  But, I have no details on exactly how to do it...

Other OS's may have their own non-standard methods of doing something similar, as well...

Offline

#5 2016-09-29 02:37 PM

malati
Member
Registered: 2016-09-27
Posts: 4

Re: Process Monitoring

yah..I got your point..and I am working on it..
but I have some more questions in my mind..

1. waitpid waits for processes which are in parent-child relationship.
but other than that ,can a process wait for any processes which are
not in parent-child relationship?
How to do that?

2.suppose I have pid of X process?
how to get all child processes of that X process..even if X process get exited ,how to get information (like pid) of its child processes..?

Offline

#6 2016-09-29 06:21 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,827
Website

Re: Process Monitoring

1. No, except for exceptions like ptrace() or the aforementioned PR_SET_CHILD_SUBREAPER...

2. How to obtain a process tree will vary from system to system...  On Linux, you would read through "/proc/<pid>/", probably looking at "PPid:" in "status"...  But, it's only going to work while the parent is still running, because once the parent exits, its orphaned children all become inhereted by init, so their PPids will all become 1...

The only way I know of to obtain the PIDs of your child process' children as they're fork()'d is via ptrace()...

Offline

#7 2016-10-02 11:13 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: Process Monitoring

You can also create a new session with setsid() and use waitpid() to wait for the process group (negative pid).

However, the shell script probably creates its own process group, and then the above won't work.

Offline

Board footer

Powered by FluxBB