Crontab not running ruby script

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.


I've gotten it working using

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


This is quite old but thought I'd comment anyway. RVM creates a wrapper for each version of ruby you install.

run this command replace 2.5.1 with your ruby version

rvm env --path 2.5.1

this gives you the path to the environment mine is

/home/username/.rvm/environments/2.5.1

you want just the rvm path

/home/username/.rvm/

take a look at the wrappers directory there should be a wrapper for each version and gemset you have installed. Test your script with the wrapper

/home/username/.rvm/wrappers/ruby-2.5.1/ruby /home/username/scripts/script.rb

or if you have a specific gemset

/home/username/.rvm/wrappers/ruby-2.5.1@gemset/ruby /home/username/scripts/script.rb

Use this command directly in your crontab

* * * * * /home/username/.rvm/wrappers/ruby-2.5.1/ruby /home/username/scripts/script.rb

Tags:

Ruby

Crontab