How to protect my process from killing?

We have a critical server program on top of Linux, and we don’t want others to accidentally terminate it. If someone stops it or it works, we want it to reboot.

So, we plan to write another program, say, program B. We want program B and the server program to protect each other. If our server program is completed, program B will restart it. If program B terminates, the server program will start it again. But we do not have a good mechanism to notify program B and the server program when her colleague leaves.

+6
source share
4 answers

You can use init for the babysit process, and since init only ends when you reboot, you don't need "program B".

Add to the end of / etc / inittab:

x:3:respawn:/path/to/my/program 

Information on syntax and other parameters can be found in man inittab

+10
source

You can reboot your server from the inside using fork. About the beauty of Unix.

Sort of:

 int result = fork(); if(result == 0) DoServer(); if(result < 0) { perror(); exit(1); } for(;;) { int status = 0; waitpid(-1, &status, 0); if(!WIFEXITED(status)) { result = fork(); if(result == 0) DoServer(); if(result < 0) { puts("uh... crashed and cannot restart"); exit(1); } } else exit(0); } 

EDIT:
It is probably wise to use the WIFEXITED macro as a test condition, which is more concise and portable (correspondingly modified code). Furthermore, it correctly models the semantics that we probably want.

waitpid , given zero flags, will return nothing but both normal and abnormal termination. WIFEXITED results in true if the process terminated normally, for example, returning from main or calling exit . If the process crashes (for example, because you requested it), it is perhaps very important that you do not continue to restart it until the end of days!

+5
source

Could a system like http://supervisord.org/ not be vialble for you? We are observing several processes, and I can confirm its features. it is very good if it will work for your application.

+4
source

They would have to interrogate each other, as a rule. Ask them to send a zero signal to each other (which simply checks for the presence of a virus and does not interrupt another program).

 echo $$>$1 read otherpid < $2 while :; do while kill -0 $otherpid do sleep 1 done # restart other program # (really restarting myself in my peer configuration) $0 $2 $1 & newpid=0 while [ "$newpid" -eq "$otherpid" ] do sleep 2 read newpid < $2 done otherpid=$newpid done 

You could think and try to do things as a watchdog to make sure that the program not only exists, but also works.

+2
source

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


All Articles