PID files around daemons after server restart

I have some daemons that use PID files to prevent my program from running in parallel. I set up a signal handler to capture SIGTERM and perform the necessary cleanup, including the PID file. This works great when I test using "kill -s SIGTERM #PID". However, when I restart the server, PID files still hang around preventing daemons from starting. As I understand it, SIGTERM is sent to all processes when the server is shutting down. Should I pick up another signal (SIGINT, SIGQUIT?) In my daemon?

+3
source share
3 answers

Use flock(or lockf) in your pidfile, if it succeeds, you can rewrite the pidfile and continue.

This SO answer provides a good example of how this is done.

+3
source

This is not a straightforward solution, but it might be a good idea to check the actual process running with pid in the pid file at startup, and if it does not exist, clean the obsolete file.

Perhaps your process receives SIGKILL before it can clear the pid file.

+3
source

, SIGTERM ​​ ( 2 3 ), SIGKILL. /etc/rc.d/rc0.d/S01halt ( ).

, Fedora 11 :

action $"Sending all processes the TERM signal..." /sbin/killall5 -15
sleep 2
action $"Sending all processes the KILL signal..."  /sbin/killall5 -9

, , , !

+2
source

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


All Articles