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.

Saicharan S M
  • 828
  • 3
  • 11
  • 25

5 Answers5

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`
saihgala
  • 5,724
  • 3
  • 34
  • 31
  • Not working.. I tried to run the shell script on the terminal it said Can't open /home/mark/.bash_profile – Saicharan S M Aug 22 '12 at 05: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 Aug 22 '12 at 06: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. – Saicharan S M Aug 22 '12 at 07:03
  • and does it work when you run it as mark user? Also FYI - `su ` will not load `'s` environment but `su - ` does load it. – saihgala Aug 22 '12 at 07:10
  • No still the same set of errors. The user does get changed to mark but the errors remain. – Saicharan S M Aug 22 '12 at 07:26
  • oci8lib.c:117:in oci8lib.so: OCI Library Initialization Error (OCIError) – Saicharan S M Aug 22 '12 at 10:21
  • See the problem is i have ruby installed in my user environment i.e as user mark. But its not installed in root user. When i try to run a ruby script as a root user it says ruby command not found. So the problem is to get ruby script running in mark's user environment (where ruby and all related packages are installed) from root (or as a root user). Please help. – Saicharan S M Aug 23 '12 at 05:02
  • why don't you run cron job as mark user? And even if you have to run it as root for some reason you can temporarily extend the path in ruby_wrapper_sh to include ruby interpreter. Do `export PATH=/path/to/your/ruby/interpreter:${PATH}` before you invoke `ruby /home/mark/project/script.rb` command – saihgala Aug 23 '12 at 05:15
  • No sorry.. I think its some pron related to my application... I'm trying to run the script on my comp(localhost) but it'll eventually run on a server. So i'll try to run it on the server directly and check if it works.. But thanks a lot for your help :) really appreciate it. If it doesn't work i'll get back to you. – Saicharan S M Aug 23 '12 at 09:20
  • It worked for me.. Whatever u said helped i.e sourcing the bash profile. I just had to run it on the server.. – Saicharan S M Aug 25 '12 at 08:43
  • mine did not work with the single qoutes around the last line (the ruby command). Is that a typo? – n00b Jul 22 '13 at 01:15
  • @n00b these are back-ticks (`) and not single quotes (') – saihgala Jul 22 '13 at 04:42
  • The backticks around the command are almost certainly wrong in the general case. If *your* Ruby script prints a command that Bash needs to evaluate, that's great fun, but the majority of people finding this answer via Google should **definitely not** be doing that. – tripleee Mar 12 '18 at 11:32
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

muhammadn
  • 330
  • 3
  • 18
  • 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 Sep 11 '15 at 07:11
  • amazing, thanks for pointing that out! and can confirm that command still works in late 2021 :) – dax Dec 19 '21 at 11:39
3

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

Krishna Srihari
  • 714
  • 6
  • 6
  • I use this. But this ultimately writes a job in the crontab. And i have a problem in running the script from cron tab. – Saicharan S M Aug 22 '12 at 05: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 Aug 22 '12 at 06: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 – Saicharan S M Aug 22 '12 at 06: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

rosada
  • 31
  • 6
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.

Ashen One
  • 371
  • 1
  • 12