Cron job not working

software development

#1

I have a ruby script that works just fine on Dreamhost’s servers, but when it is run by cron, it fails with:

/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require': no such file to load -- hpricot (LoadError) from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
from /home/hydromint/lfdb.hydromint.com/current/get_new_listings.rb:7

At the top of my script, I have:
#!/usr/bin/env ruby

I need to add these to the require search path

$: << “./vendor/hpricot-0.5/lib”
$: << "./vendor/mechanize-0.6.5/lib"
require "rubygems"
require 'hpricot’
require "mechanize"
require "logger"
require “mysql”

Again, when I run the script with ./get_new_listings.rb ON THE SERVER, it completes successfully, but every cron report gives me the above error. Any ideas?


#2

Do you need to include your environment in your script? Cron by default uses a minimized environment so you might lose a lot of your library pointers.

Wholly - Use promo code WhollyMindless for full 97$ credit. Let me know if you want something else!


#3

I’m not sure. How would I do that? I’ll give it a shot.


#4

with bash (the default shell) you’d probably include a line that shows “. .bashrc” or it might be your .profile that you should pull.

The “dot” says basically “Pretend the commands in this file were typed here”

Be careful, you can accidentally endanger your cron script’s security by including paths you don’t actually want. Hardcore admins will either type the entire path to commands or add the necessary path requirements in the cron script itself to avoid surprises if the .bashrc/.profile is changed later or something is added to the path.

Wholly - Use promo code WhollyMindless for full 97$ credit. Let me know if you want something else!


#5

OK, I think I’ve figured it all out. Notice that at the top of my script, I was loading ruby through “#!/usr/bin/env ruby”. After running through the hash of Ruby environment settings:


#!/usr/bin/ruby
ENV.each {|key, value| puts “#{key} is #{value}”}

I noticed that the _ variable for “#!/usr/bin/env ruby” scripts is “/usr/bin/env” and “/usr/bin/ruby” for “#!/usr/bin/ruby” scripts. I hope that makes sense for anyone else having problems similar to mine. That tipped me off to try loading ruby through a straight call ("#!/usr/bin/ruby"). It seems to have worked.

Thanks for your help, wholly.
-geoffrey


#6

Good catch. Thanks for the update, surely someone will run into that too. It’s common to call a script from cron to run other applications and languages so you don’t get caught with that kind of problem. Scripts almost always start

Wholly - Use promo code WhollyMindless for full 97$ credit. Let me know if you want something else!