views:

178

answers:

2

I have a cron job to run a Ruby script, which runs fine on the command line (from user "user"), but the cron fails because it cannot find a ruby gem TMail. I manually installed the tmail gem to my ~/.gems/ dir, pointed to by my ~/.bashrc script, as suggested by Nate.

When invoked manually from the shell (on deadpool server)

./home/user/site.com/appdir/testbalances.sh

works fine, but from the CRONtab web-interface (same command), I get a log mail:

/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require': no such file to load -- tmail (LoadError) from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in require' from afschriftmonitor/statement_monitor.rb:7

Lines 6 and 7 of the file afschriftmonitor/statement_monitor.rb:7 are

require 'rubygems' require 'tmail'

It seems that CRON is running from another server, or it is ignoring the ~/.bashrc file? How can I fix this? It seems to be a Dreamhost hosting specific issue, because the script runs fine locally on my Mac and - as said - fine from my shell account on Dreamhost. I am puzzled.

A: 

These kinds of problems are invariably a mismatch between your shell environment and cron's environment. See this page for various suggestions: Does your cron script run in a bash shell, but not while running under crontab?

glenn jackman
Nice suggestion, I tried it and added .bashrc into the bashscript that invokes the ruby call. Didn't help :-(
Felix Ogg
Actually it did. There was confusion about the server it ran on at Dreamhost, which led to mixed signals, but this answer solved the original problem. Thanks for your comments on my answer!
Felix Ogg
+1  A: 

Fixed it myself: turned out it is/was a 2-part problem:

  • Dreamhost runs it's CRON (as any other CRON AFAIK) from the sh shell, whereas my console testing was from Bash shell.
  • CRON loads different environment settings than your console shell, as Glenn pointed out.

Combining these I managed to fix the problem by:

  1. Duplicating my ~/.bashrc into a ~/.shrc file, (set to chmod +x)
  2. Adding the following line to the top of my bash script, ignited from CRON:

    . ~/.shrc

Checking for the right environment is simple by adding a call to env inside the script.

Felix Ogg
If you're sourcing a file, it isn't necessary for it to be executable, just readable.
glenn jackman
It can be instructive to run "env;set" as a cron job to see what the server's environment is.
glenn jackman