Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PID files hanging around for daemons after server restart

I have some daemons that use PID files to prevent parallel execution of my program. I have set up a signal handler to trap SIGTERM and do the necessary clean-up including the PID file. This works great when I test using "kill -s SIGTERM #PID". However, when I reboot the server the PID files are still hanging around preventing start-up of the daemons. It is my understanding that SIGTERM is sent to all processes when a server is shutting down. Should I be trapping another signal (SIGINT, SIGQUIT?) in my daemon?

like image 628
stinkypyper Avatar asked Jan 19 '26 06:01

stinkypyper


1 Answers

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

This SO answer has a good example on how this is done.

like image 83
Hasturkun Avatar answered Jan 20 '26 21:01

Hasturkun