3

I have ruby daemons running. Once in a while we'll accidentally start up a second instance of it, which causes race conditions.

Can I use Monit to detect if multiple instances of 'my_ruby_daemon' is running? And if so kill the extra instance?

1 Answers1

3

I don't know if you can do this with monit, but it strikes me as the Wrong Solution which may bring up its own problems (especially if Bad Things happen if you kill the "wrong" daemon (?)).

The Right Solution is to have your daemon create and check a lock (PID files work great for this -- if mydaemon.pid exists and the PID in it is alive refuse to start the daemon).
There are several ruby lockfile gems/libraries/etc available (like this one).
If modifying the daemon is out of the question almost every Unix system comes with the lockfile command or something similar -- wrap the daemon in a shell script that creates/checks the lock before attempting to start.

It helps if you put the lock file somewhere that gets cleared on reboot (/tmp, /var/run, etc.) so that if your system crashes the daemon will restart when its init script runs.

voretaq7
  • 79,345
  • 17
  • 128
  • 213