0

I have a ruby script that I have tested to be working that I would like to run as an hourly cron but cannot seem to get it firing properly.

The last thing I have tried was placing the line:

ruby ~/ruby_script.rb

in /etc/cron.hourly

Said ruby script is located in the home directory with:

#!/usr/bin/env ruby

as its top line.

I have looked into ruby & cron resources but they most seem to be for reoccurring tasks in a Ruby on Rails environment when I just want the script to run in my ubuntu environment. I have double checked that rails is installed as well.

I have had a lot of fun learning more about ubuntu over the past few months and will truly appreciate any assistance I receive here. Thank you in advance.

Erik
  • 898
  • 2
  • 8
  • 28
  • Check this tutorial on crontab out: https://help.ubuntu.com/community/CronHowto Also see this previous Stack Overflow question: http://stackoverflow.com/questions/3984134/cron-that-will-run-a-ruby-script-every-day-at-midnight Hope it helps. – O-I Dec 26 '13 at 19:28
  • cron runs with a limited environment. PATH may not be set as you expect, so /usr/bin/env wont find it, even if the cron is able to find the script. Suggest changing `ruby` in cron.hourly to the absolute path of the `ruby` interpeter – Chaim Geretz Dec 26 '13 at 20:00
  • Also check your system log to see any error messages that cron might be printing when running your command. – Max Dec 26 '13 at 21:43
  • I have changed 'ruby' to 'path/to/ruby' based on the directory from 'which ruby' will update in a few hours. – Erik Dec 27 '13 at 00:57
  • Hmmm. Having the line path/to/ruby path/to/script isn't working. Any other ideas? – Erik Dec 27 '13 at 18:02
  • if you are using rvm please see http://stackoverflow.com/questions/7219400/rvm-isnt-setting-environment-with-cron – Малъ Скрылевъ Dec 30 '13 at 06:09

1 Answers1

0

Try to use current user crontab

$ crontab -e

Add new cron job

0 * * * *  /bin/bash -l -c 'ruby ~/ruby_script.rb'

Using bash command to run helps to get env variables during script execution.

(for tests change 0 to * - script will try to run every minute)

To log errors you can try to add command:

0 * * * *  /bin/bash -l -c 'ruby ~/ruby_script.rb >> ~/ruby_script.log 2>&1 &'

Hope it helps.

Jax
  • 1,839
  • 3
  • 18
  • 30