Making monit, delayed_job, and bundler play nice together

words by Brian Racer

Recently I was having a heck of a time getting monit to start my delayed_job instances. I was using the monit template that came with delayed job, it looks something like this:

check process delayed_job_bandwith_prod 
  with pidfile /home/bandwith/apps/production/shared/pids/delayed_job.pid
  start program = "/usr/bin/env RAILS_ENV=production /home/bandwith/apps/production/current/script/delayed_job start" as uid bandwith and gid bandwith 
  stop program  = "/usr/bin/env RAILS_ENV=production /home/bandwith/apps/production/current/script/delayed_job stop" as uid bandwith and gid bandwith

This did not work however, and after quite a bit of debugging I found there are a couple of issues you might need to be aware of:

1. Your $PATH

monit starts things with a ‘spartan path‘ of:

/bin:/usr/bin:/sbin:/usr/sbin

My ruby happens to be in /usr/local/bin, so we will need to pass that in too:

start program = "/usr/bin/env PATH=/usr/local/bin:PATH RAILS_ENV=production /var/www/apps/{app_name}/current/script/delayed_job start"

2. monit doesn’t define a $HOME environment variable

Even though we are starting these processes with uids and guids specified, that doesn’t actually load the users shell. With no $HOME env variable, bundler can’t find where your gems are installed and thinks they are all missing. I ended up just putting the variable in the monit command, another option might be running su -c ‘{command}’ (I didn’t test that).

So putting that all together you get the following which should make everything work:

check process delayed_job_bandwith_prod 
  with pidfile /home/bandwith/apps/production/shared/pids/delayed_job.pid
  start program = "/usr/bin/env HOME=/home/bandwith PATH=/usr/local/bin:$PATH RAILS_ENV=production /home/bandwith/apps/production/current/script/delayed_job start" as uid bandwith and gid bandwith 
  stop program  = "/usr/bin/env HOME=/home/bandwith PATH=/usr/local/bin:$PATH RAILS_ENV=production /home/bandwith/apps/production/current/script/delayed_job stop" as uid bandwith and gid bandwith

I would be interested to know if anyone has any better suggestions, but this seems to be working nicely.

  • Andreas Haller

    Thank you for sharing this! I am also using “The Bundler” (and daemon-kit) but didn’t think about setting HOME and PATH variable. Now it just works. Awesome.

  • Andreas Haller

    Thank you for sharing this! I am also using “The Bundler” (and daemon-kit) but didn’t think about setting HOME and PATH variable. Now it just works. Awesome.

  • Thank You. The moral of the story: Now your environment! 🙂
    This is what monit will live you with:
    {“MONIT_DATE”=>”Thu, 24 Jun 2010 10:40:36 +0000”, “MONIT_HOST”=>”webtrans2.dh.bytemark.co.uk”, “PATH”=>”/bin:/usr/bin:/sbin:/usr/sbin”, “PWD”=>”/”, “MONIT_PROCESS_PID”=>”0”, “MONIT_EVENT”=>”Started”, “MONIT_PROCESS_MEMORY”=>”0”, “SHLVL”=>”1”, “MONIT_PROCESS_CPU_PERCENT”=>”0”, “MONIT_SERVICE”=>”backgroundrb_11001”, “MONIT_PROCESS_CHILDREN”=>”0”, “_”=>”/usr/bin/ruby”}

    Anything else you must add it on your own.

  • Thank You. The moral of the story: Now your environment! 🙂
    This is what monit will live you with:
    {“MONIT_DATE”=>”Thu, 24 Jun 2010 10:40:36 +0000”, “MONIT_HOST”=>”webtrans2.dh.bytemark.co.uk”, “PATH”=>”/bin:/usr/bin:/sbin:/usr/sbin”, “PWD”=>”/”, “MONIT_PROCESS_PID”=>”0”, “MONIT_EVENT”=>”Started”, “MONIT_PROCESS_MEMORY”=>”0”, “SHLVL”=>”1”, “MONIT_PROCESS_CPU_PERCENT”=>”0”, “MONIT_SERVICE”=>”backgroundrb_11001”, “MONIT_PROCESS_CHILDREN”=>”0”, “_”=>”/usr/bin/ruby”}

    Anything else you must add it on your own.

  • Know your environment! 🙂
    Sorry

  • Know your environment! 🙂
    Sorry

  • Simon

    Thanks for this – had got as far as fixing the path, but would never have thought of giving it a HOME variable.

  • Pingback: Monit – Easy Monitoring | Modus Create()

  • Pingback: sorty | so nice, i want..()