Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've set a crontab for my ruby script.

crontab -l
12 10 * * * /bin/bash  -c 'cd /home/user/path/ && /home/user/.rvm/rubies/ruby-1.9.2-p136/bin/ruby  -rubygems script.rb'

the command /bin/bash -c 'cd /home/user/path/ && /home/user/.rvm/rubies/ruby-1.9.2-p136/bin/ruby -rubygems script.rb' runs well.

And in cron, cron is executing the command (I checked the syslog:

sudo tail  /var/log/syslog
(user) CMD (/bin/bash  -c 'cd /home/user/path/ && /home/user/.rvm/rubies/ruby-1.9.2-p136/bin/ruby  -rubygems script.rb')

)

But the script is not executing exactly. I'm not getting the expected out put from it.

What may be the cause of the issue? It'll be grate if some one can help me.

share|improve this question

3 Answers

up vote 5 down vote accepted

Try:

12 10 * * * /bin/bash  -c 'source /home/user/.rvm/scripts/rvm && cd /home/user/path/ && /home/user/.rvm/bin/ruby-1.9.2-p136 -rubygems script.rb'
share|improve this answer
2  
Good answer. When you login to bash from a terminal it automatically runs some scripts to setup your environment (usually including the "rvm" ruby environment setup). "cron" just runs exactly what it finds in the cron file and does not automatically run any environment setup scripts. – James Anderson Aug 8 '11 at 6:35
thank you scuawn, Could you tell me what the source does? – Sayuj Aug 8 '11 at 6:44
1  
@Sayuj: That just runs all the usual rvm initialization stuff that you'd normally get with a login shell. – mu is too short Aug 8 '11 at 7:03
Good answer. Here is a full blog post on the subject: danielsz.posterous.com/how-to-run-rvm-scripts-as-cron-jobs – Daniel Szmulewicz Oct 17 '11 at 0:54

Try running env -i $SHELL --norc in terminal and then run the ruby script. The terminal will have the same environment as the cron

share|improve this answer

If you are using .rvmrc file in your project to setup ruby version for you project. Then you need to tell rvm to trust rvmrc file in ~/.rvmrc

rvm_trust_rvmrcs_flag=1

this will disable prompt and your cron will not hang.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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