Hung Truong: The Blog!

Setting The Crontab Environment For Your Ruby on Rails Jobs

June 08, 2008 | 1 Minute Read

So today is my birthday. In honor of my birthday, I’ve been migrating some websites from my old server to a new one. Okay, it’s not really in honor of my birthday; I’m just doing it because I have some free time today.

Anyway, one of the things that Anime Nano needs to do is run a crontab of a function from a Rails model using a crontab. If that made no sense to you, don’t worry. Just enjoy the pretty picture of the rails logo above. If that made sense to you, wow, you’re pretty hardcore.

This is something that I remember spending time getting to work the last time I set up my server. But unfortunately I didn’t write about how I did it, so I had to spend an hour or so figuring it out again. Apparently, crontab runs in a different environment than the root user. If you try to run a script for Ruby using “ruby script/runner” you’ll just get nothing. No error message, just nothing. I did something on the previous server that set the crontab environment, but I have no idea what I did. I searched. I didn’t find the answer.

So what I ended up doing (this is for future Hung when he has to eventually move his sites to an even newer server) is sticking some lines for my environment into the crontab itself. I just ran ‘env’ and got a bunch of stuff. Comparing the ‘env’ command run from the crontab itself (I just piped the output to a random text file) and running in my shell, I saw a few differences. Most notably these ones:




And voila, my crontabbed rails script/runner model script thing ran. Hopefully writing this post will save me some time in the future.

Oh, and happy birthday to me.