Question

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:inrequire' 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.

Was it helpful?

Solution

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?

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top