Design a monitoring process for monitoring and restarting processes

I am designing a monitoring process. The purpose of the monitoring process is to track several configured processes. When the monitoring process detects that the process has gone down, it needs to restart the process.

I am developing code for my Linux system. This is how I developed a small prototype - the Fed details (path, arguments) about the various processes that need to be controlled. - The monitoring process did the following: 1. A signal handler for SIGCHLD was installed 2. A plug and execv to start the process to be monitored. Store the pid of child processes. 3. When the child descends, the parent receives SIGCHLD 4. Now the signal handler will be called. The handler will start the for loop in the list of previously saved files. For each pid, it checks the / proc file system for a directory matching pid. If the directory does not exist, the process restarts.

Now, my question is: - Is the above method (for checking the / proc file system) a standard or recommended mechanism for checking the functioning of the process, or should I do something like creating a channel for the ps command and scrolling the ps output? - Is there a better way to achieve my requirement?

Sincerely.

+2
source share
4 answers

You do not have to check /procto determine which process has left it - it is possible for another, unrelated process to go to this time and, by coincidence, assign the same PID.

Instead, in your handler, SIGCHLDyou should use a system call waitpid()in a loop, for example:

int status;
pid_t child;

while ((child = waitpid(-1, &status, WNOHANG)) > 0)
{
    /* Process with PID 'child' has exited, handle it */
}

( , , SIGCHLD).

+4

, . , on/proc SIGCLD, , , ?

,... , ,

while((pid = waitpid(-1, &status, WNOHANG))) SIGCLD Pid Wxxx .

, wait() waitpid() . , /proc, , .

+2
+1
source

You can easily find out if a process is alive by calling a system call kill()on its pid. If the child is not alive, kill()it will fail.

In addition, a call waitpid()with the option WNOHANGimmediately returns zero if the process is still alive.

IMHO, reading proc or piping to ps files is a nasty way to do this.

0
source

Source: https://habr.com/ru/post/1788300/


All Articles