Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a cron job to run a ruby script?

I want to create a cron job to run a ruby script. this is what i have put in the crontab.

2 * * * * ruby /home/mark/project/script.rb >> /home/mark/cronOutput.txt

But its not running. I think there's some problem with the environment getting loaded up when the cron runs as root.

Please help.

like image 753
Saicharan S M Avatar asked Aug 22 '12 04:08

Saicharan S M


2 Answers

If you are using RVM, you can simply do:

rvm cron setup 

Reference: https://coderwall.com/p/vhv8aw/getting-ruby-scripts-working-with-bundler-rvm-and-cron

like image 30
muhammadn Avatar answered Oct 31 '22 17:10

muhammadn


If your ruby is in non standard paths then personally I like to wrap my ruby calls in a shell script, thereby ensuring that all the paths etc. my ruby program needs are set correctly, and schedule the script in crontab. Do something like

2 * * * * /home/mark/project/ruby_wrapper_sh >> /home/mark/cronOutput.txt 2>&1

and your /home/mark/project/ruby_wrapper_sh should read something like

#!/bin/bash

. ~mark/.bash_profile 
`ruby /home/mark/project/script.rb`
like image 136
saihgala Avatar answered Oct 31 '22 18:10

saihgala