3

I have a ruby process that don't run as a daemon (it runs in the foreground), but can generate a pid file. My question is does Monit always expects to monitor background/daemonized processes?

I used Bluepill in the past, and that was the default behaviour:

Note that since we specified a PID file and start command, bluepill assumes the process will daemonize itself. If we wanted bluepill to daemonize it for us, we can do (note we still need to specify a PID file): process.daemonize = true

Is there something similar for monit? AFAIK, as long as the process (daemon or not) can create a PID file, Monit will detect if it daemonize itself and won't daemonize it and vice versa...

Thank you

3

It will work fine for monit.. You can have it check the pid file of the process and basically what monit is doing is doing a

ps aux | grep pid#

If it's there it think's it's up and running. If you don't have a pid file you can check a process also via something like

check process myprocessname
    matching "myprocessname"

That pretty much does a

ps aux | grep myprocessname
  • Hi Mike, thanks for your reply. Could you give me a bit more details about how monit run de processes? basically, I can pass the option to my process to run as a daemon or not. Does monit daemonize process automatically? I'm wondering what's the behaviour of monit about daemonizing, comparing to BluePill (like the quote in my question) – allaire Jul 15 '13 at 1:55
  • From what I've read, monit differs from Bluepill or God.rb a bit since it expects a daemon process to watch vs Bluepill and God can also daemon processes. I should probably use Upstart if I need to daemonize a process. Are my assumptions correct? – allaire Jul 15 '13 at 3:56
  • monit doesn't care if it's a daemon or not.. it just cares if it can match what you tell it to match.. so both of my examples are valid ad I'd use a normal pid example since your process writes out a pid file – Mike Jul 15 '13 at 13:02
  • Yes, but I suppose monit, when it starts back a process, and the process is not a daemon or does not run in the background by itself, it (monit) must be responsable for detaching it to the background? Like using nohup and the ampersand (&) if it detects it needs to do it? – allaire Jul 15 '13 at 14:03
1
+50

Easy way is to deamonize your non-deamon proces. That is, create shell script:

#!/bin/sh
/usr/local/bin/your_program < /dev/null &

And then use that script as a daemon to run. It will start your program (which will create PID file) in background, and then will return control immediately, thus avoiding timeout and spurious killing by monit.

  • "which will create PID file" - Could you elaborate on that? I don't follow. – Hubro Sep 1 '16 at 21:25
  • Hubro: original question asked "I have a ruby process that don't run as a daemon (it runs in the foreground), but can generate a pid file.", which I just referenced in the answer. If your (as oposed to orignal posters) process does not generate pid file by itself, you can add to the end of above shell script line echo $! > /var/run/your_program.pid and it will be created. – Matija Nalis Sep 1 '16 at 21:32
  • 1
    I used your advice plus nohup and sudo -u www-data -g www-data in order to solve this, and it appears to work. – Hubro Sep 1 '16 at 22:21
  • So monit can't do this on its own then? – Hubro Sep 7 '16 at 9:22
  • @hubro I don't use monit that much (I use DJBs daemontools mostly), so I wouldn't know really... Perhaps the docs or examples mention better way of doing it – Matija Nalis Sep 7 '16 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.