Running ruby cron jobs on when using rvm

2986882105_70e52e3516

This gave me a headache for a couple hours, so once again, my pain is your gain. If you’re using RVM and running cron jobs that are ruby scripts, you must remember that:

unlike system ruby, cron jobs don’t run .bashrc so rvm is not loaded 

That means when you tell it to run ruby, cron has no idea where ruby is, and just fails silently. So how do you tell it to load ruby through rvm? Well, you load bash and tell it to run something:

RAILS_ENV=production

04 * * * * /bin/bash -l -c 'cd /absolute/path/to/my/script && ruby myscript.rb'

Notice that you can set environment variables in the cron file above your scheduled scripts. Also, in your script, make sure you’re not writing things to stdout using puts(). That’ll give you Broken Pipe errors when you’re running your cron job.

Advertisements

6 thoughts on “Running ruby cron jobs on when using rvm

  1. smart! I was doing:su – USER -c “command”but that required to change sudoers permissions for the cron userThis is sooo much better! Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s