16

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.

3
  • Are you willing to execute code in rails then write a rake task and execute it inside cron. Commented Aug 22, 2012 at 5:23
  • Anything in the con need full address which ruby, which script and where info. Commented Mar 1, 2016 at 11:18
  • use active jib sidekiq Commented Apr 4, 2017 at 9:50

5 Answers 5

15

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`
13
  • Not working.. I tried to run the shell script on the terminal it said Can't open /home/mark/.bash_profile Commented Aug 22, 2012 at 5:56
  • I have just given you an example, your user account might be set up using a .profile or /etc/profile file only. You'll need to change this line according to your environment, before running the script. As long as you're able to fix and execute this script from command line, you should have no issues running it through cron
    – saihgala
    Commented Aug 22, 2012 at 6:13
  • I tried running the script as a root via the terminal. I used su to switch to root. Getting many OCI errors. The environment present in mark user is not getting loaded when i switch to root. Thats the prob. Commented Aug 22, 2012 at 7:03
  • and does it work when you run it as mark user? Also FYI - su <user> will not load <user>'s environment but su - <user> does load it.
    – saihgala
    Commented Aug 22, 2012 at 7:10
  • No still the same set of errors. The user does get changed to mark but the errors remain. Commented Aug 22, 2012 at 7:26
7

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

2
  • 2
    Beware that this command replaces your crontab, if you have some cronjobs there, remember to backup them by using crontab -l > somefile.txt
    – Zequez
    Commented Sep 11, 2015 at 7:11
  • amazing, thanks for pointing that out! and can confirm that command still works in late 2021 :)
    – dax
    Commented Dec 19, 2021 at 11:39
3

Check whenever(https://github.com/javan/whenever) gem to use cron jobs in Rails

3
  • I use this. But this ultimately writes a job in the crontab. And i have a problem in running the script from cron tab. Commented Aug 22, 2012 at 5:56
  • Can you explain the issue you are having in running the task from the crontab? You can check /var/log/syslog to see if your methods in the crontab are actually running.
    – sandeep
    Commented Aug 22, 2012 at 6:15
  • All i want to do is to run a ruby script every 5 mins or so.. I'm not able to do that. And yes i checked syslog my cron job to run the ruby script is getting executed Commented Aug 22, 2012 at 6:46
2

working on centos

in your terminal execute # which ruby which is find your ruby path

example output

/usr/bin/ruby

Then you can edit your cronjob, using crontab -e

* * * * * /usr/bin/ruby /home/mark/project/script.rb

and save, this simply working on my centos server. You can test the code first using this command before you edit your cronjob

#/usr/bin/ruby /home/mark/project/script.rb

it should be working first, then you can put on your crontab

2
  • 1
    You should elaborate the answer. Commented Feb 28, 2018 at 3:26
  • Oh, okey, sorry
    – rosada
    Commented Feb 28, 2018 at 3:32
1

Thanks for @saihgala for his solution, but I'm little modified this way. I add #!/usr/bin/env ruby sting to the beginning of my ruby executable file.

Add permissions for this file, launch crontab file edit crontab -e.

Add */1 * * * 0-5 /path/to/executable.rb>>/path/to/output.txt and then it works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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