简体   繁体   中英

Ruby Cron job not running?

My cron job won't finish. And I've been spinning my wheels these past couple hours with no luck.

I have this line in my crontab:

* * * * * /usr/local/rvm/rubies/ruby-2.0.0-p195/bin/ruby /home/pra/grinder/misc_scripts/daily_total_logger.rb

I can run the command:

/usr/local/rvm/rubies/ruby-2.0.0-p195/bin/ruby /home/pra/grinder/misc_scripts/daily_total_logger.rb

and it works no problem.

I set it to run every minute so I can watch the logs and sure enough I see this in the logs (/var/log/cron):

Dec 19 01:32:01 hostname CROND[4574]: (root) CMD (/usr/local/rvm/rubies/ruby-2.0.0-p195/bin/ruby /home/pra/grinder/misc_scripts/daily_total_logger.rb)

I have replicated this same behavior on two separate servers (after fooling with the script for an hour I figured I'd just move it to another server, but same problem there).

I have tried one more thing. To create another ruby file with the same permissions that does something else (append the date time to a file). I created /home/pra/grinder/misc_scripts/test.rb with the following code

open('/home/log.log', 'a') do |f|
  f.puts Time.now.to_s
end

And it works just fine. A quick check of the permissions shows that both files have idential permissions

-rw-r--r--. 1 root root 348 Dec 19 00:52 daily_total_logger.rb
-rw-r--r--. 1 root root  61 Dec 19 01:13 test.rb 

I know it's best practice not to run this in root, I'm just trying to eliminate all potential issues. I can't for the life of me figure out what could be causing this. I simplified the script and I still have the same error with this super simple script:

require 'pg'

conn = PG.connect(dbname: '***',user: '***',password: '***', host: '***')
query = "INSERT INTO foobar (foo, bar) VALUES (1, 3)"
conn.exec(query)

puts "Done..."

And it works fine if I run it from the command line. Just won't complete if I run it as a cron job. I'm totally baffled - Same issue on two servers, one debian, one redhat... Any help would be highly appreciated.

I traced it down to this line:

require 'pg'

If I require that library in my script, the rest of the script stops running when called as a cron job, but works fine when called from the command line. For more information on this, I documented the problem a lot better here (and hopefully someone will shed light on why this is happening):

Requiring a Ruby Gem in Ruby Script breaks Cron Job Execution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM