Jaco Pretorius
Jaco Pretorius

Reputation: 24840

Cron job can't load gem

I have a ruby script that connects to an Amazon S3 bucket and downloads the latest production backup. I have tested the script (which is very simple) and it works fine.

However, when I schedule this script to be run as a cron job it seems to fail when it loads the Amazon (aws-s3) gem.

The first few lines of my script looks like this:

#!/usr/bin/env ruby
require 'aws/s3'

As I said, when I run this script manually, it works fine. When I run it via a scheduled cron job, it fails when it tries to load the gem:

`require': no such file to load -- aws/s3 (LoadError)

The crontab for this script looks like this:

0 3 * * * ~/Downloader/download.rb > ~/Downloader/output.log 2>&1

I originally thought it might be because cron is running as a different user, but when I do a 'whoami' at the start of my ruby script it tells me it's running as the same user I always use.

I have also done a bundle init and added the gem to my gemfile, but this doesn't seem to have any affect.

Why does cron fail to load the gem? I am running Ubuntu.

Upvotes: 13

Views: 6969

Answers (6)

SimonS
SimonS

Reputation: 926

I've tried all the solution above, none of them worked until I tried;

0 12 * * * /bin/bash -l -c 'ruby /Users/simon/Desktop/script.rb'

Upvotes: 0

Michael
Michael

Reputation: 1962

As mentioned here https://coderwall.com/p/vhv8aw you can simply try

rvm cron setup # let RMV do your cron settings

Make sure that you make copy of your crontab before running this command

Upvotes: 24

Templum Iovis
Templum Iovis

Reputation: 67

Add this at the beginning of your cron

PATH="/home/user/.rvm/gems/ruby-2.1.4/bin:/home/user/.rvm/gems/ruby-2.1.4@global/bin:/home/user/.rvm/rubies/ruby-2.1.4/bin:/home/user/.rvm/gems/ruby-2.1.4/bin:/home/user/.rvm/gems/ruby-2.1.4@global/bin:/home/user/.rvm/rubies/ruby-2.1.4/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/home/user/.rvm/bin:/usr/local/sbin:/usr/sbin:/home/user/.rvm/bin:/home/user/.local/bin:/home/user/bin"
GEM_HOME='/home/user/.rvm/gems/ruby-2.1.4'
GEM_PATH='/home/user/.rvm/gems/ruby-2.1.4:/home/user/.rvm/gems/ruby-2.1.4@global'
MY_RUBY_HOME='/home/user/.rvm/rubies/ruby-2.1.4'
IRBRC='/home/user/.rvm/rubies/ruby-2.1.4/.irbrc'
RUBY_VERSION='ruby-2.1.4'

Upvotes: 1

Derek Longmuir
Derek Longmuir

Reputation: 181

If you're running it manually and it works you're probably in a different shell environment than cron is executing in. Since you mention you're on Ubuntu, the cron jobs probably execute under /bin/sh, and you're manually running them under /bin/bash if you haven't changed anything.

You can debug your environment problems or you can change the shell that your job runs under.

To debug, There are several ways to figure out what shell your cron jobs are using. It can be defined in

/etc/crontab

or you can make a cron job to dump your shell and environment information, as has been mentioned in this SO answer: How to simulate the environment cron executes a script with?

To switch to that shell and see the actual errors causing your job to fail, do

sudo su
env -i <path to shell> (e.g. /bin/sh)

Then running your script you should see what the errors are and be able to fix them (rubygems?).

Option 2 is to switch shells. You can always try something like:

 0 3 * * * /bin/bash -c '~/Downloader/download.rb > ~/Downloader/output.log 2>&1'

To force your job into bash. That might also clear things up.

Upvotes: 12

Achilles
Achilles

Reputation: 766

You may also explicitly set your Gem path:

GEM_HOME="/usr/local/rvm/gems/ruby-1.9.2-p290@my-special-gemset"

Upvotes: 5

user904990
user904990

Reputation:

in a non cron environment execute echo $PATH, copy the path and paste it into your crontab, before your command:

echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

and inside crontab:

PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
0 3 * * * ~/Downloader/download.rb > ~/Downloader/output.log 2>&1

Upvotes: 1

Related Questions