Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crontab not running ruby script

Tags:

ruby

crontab

The crontab -l below doesn't seem to run. The script run by hand runs fine. Here is the error i'm seeing

Dec  3 20:12:01 dahlia /USR/SBIN/CRON[13912]: (gigawatt) CMD (/bin/sh -c "(export   PATH=/usr/bin:/bin; /home/gigawatt/drbronnersbot/drbronnersbot.rb)")
Dec  3 20:12:01 dahlia /USR/SBIN/CRON[13910]: (CRON) error (grandchild #13912 failed with exit status 1)

And here is the crontab:

* * * * * /bin/sh -c "(export PATH=/usr/bin:/bin; /home/gigawatt/drbronnersbot/drbronnersbot.rb)"

Permissions are fully open, its executable, i put the env path at the beginning of the file, still no dice.

like image 724
Gregg Horton Avatar asked Dec 04 '13 04:12

Gregg Horton


People also ask

What is cron job in rails?

A cron job is a process that is scheduled to run at a certain time (every minute, day, week, or month). On Unix systems, we can run and manage these types of processes using the cron tool. The info of the process that will run the cron tool is stored in a file called crontab.


2 Answers

Edit: I just noticed the user answered his own post, but I'll leave this up in case someone stumbles across here with a similar issue. I've found it's helpful for some.

I've run into this and found that this is the solution for Ruby scripts.

Ruby needs to be executed in a particular environment. RVM handles this by sourcing a ruby environment file for you particular version of ruby that sets all the required environment variables. For example if you have ruby 1.9.3 patch 448, you can look at the environment file that is sourced:

cat /usr/local/rvm/environments/ruby-1.9.3-p484

export PATH="/usr/local/rvm/gems/ruby-1.9.3-p484/bin:/usr/local/rvm/gems/ruby-1.9.3-p484@global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p484/bin:$PATH"
export GEM_HOME='/usr/local/rvm/gems/ruby-1.9.3-p484'
export GEM_PATH='/usr/local/rvm/gems/ruby-1.9.3-p484:/usr/local/rvm/gems/ruby-1.9.3-p484@global'
export IRBRC='/usr/local/rvm/rubies/ruby-1.9.3-p484/.irbrc'
unset MAGLEV_HOME
unset RBXOPT

(Note: My installation of rvm was under /usr/local/.. but yours may be elsewhere. Use which ruby to figure out where your ruby is installed)

Here you can see that it's setting the PATH and some other important environment variables. When you type rvm use ruby-1.9.3-p448, rvm actually sources this file in the background.

Cron executions are non-interactive sessions, which means they have no "live" user logged-in in front of a session. When you do it manually and run an interactive session, all this is taken care of for you, but for a non-interactive session, it doesn't know what the shell is or where to find the environment path. Maybe someone with more knowledge can provide a technical explanation as to why.

Anyways, to get around that, add this to the top of your crontab:

SHELL=/bin/bash
BASH_ENV=/home/gigawatt/.bashrc

* * * * * /home/gigawatt/.rvm/rubies/ruby-2.0.0-p247/bin/ruby /home/gigawatt/drbronnersbot/drbronnersbot.rb

This is telling the non-interactive cron user which shell to use and then telling it to source the .bashrc file. What's in the .bashrc file? Good question, you should add this line -

source /usr/local/rvm/environments/ruby-1.9.3-p484

(Once again, replace with your own ruby path) Basically you are manually sourcing the environment file that rvm would have sourced for you. It's a way of getting cron to use a particular gem environment or gemset.

It should work with those two changes.

Edit2: On Ubuntu and similar systems, the default .bashrc often contains something like

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

As the comment implies, the file wont run in a non-interactive/cron session. Anything you add under that line won't be executed.

In that case, either put your source command above that line or just use a different file all together, like ~/.cronrc for example.

like image 123
user2490003 Avatar answered Sep 19 '22 17:09

user2490003


I've gotten it working using

* * * * * /bin/bash -l -c 'ruby my-ruby-file.rb'

like image 37
hexinpeter Avatar answered Sep 20 '22 17:09

hexinpeter