Whenever not working from cron, but perfectly from terminal

2,849 views
Skip to first unread message

dipsaus...@gmail.com

unread,
Apr 15, 2012, 9:27:26 AM4/15/12
to whenev...@googlegroups.com

Using Whenever 0.7.3, Rails 3.2.2, Ruby 1.9.3, Passenger 3.0.11 on a CentOS 5.6 server (I'm not using rvm). The problem is that the cron jobs created by whenever are clearly executed, I can see them in the Cron log, however they don't actually execute the Rails code. If I copy the cron code and paste it in the terminal it works as expected and does.

The code to be executed:

class SomeModel < ActiveRecord::Base

 
#Delete all items that haven't been updated for 2 hours or more
 
def self.cron_job
   
SomeModel.destroy_all(updated_at < ?", 2.hours.ago])
  end
end

The schedule.rb:

set :output, "/var/log/cron"

every
1.day, :at => '4:45 am' do
  runner
"SomeModel.cron_job"
end

Which generated the following cron (output crontab -l):

# Begin Whenever generated tasks for: site_name
45 4 * * * /bin/bash -l -c 'cd /home/user/domains/site_name && script/rails runner -e production '\''SomeModel.cron_job'\'' >> /var/log/cron 2>&1'
# End Whenever generated tasks for: site_name

If I check the following morning I find this in the cron log:

Apr 14 04:45:01 node1 crond[9155]: (root) CMD (/bin/bash -l -c 'cd /home/user/domains/site_name && script/rails runner -e production '\''SomeModel.cron_job'\'' >> /var/log/cron 2>&1')

However the items that should have been deleted are still in the database. If I copy the code cron is supposed to execute (/bin/bash -l -c 'cd ... 2>&1' and execute it from the terminal then the items are removed as expected.

Also when I execute the code manually from the terminal there are no errors, in the cron log i see the following:

from script/rails:5:in 'require'
from script
/rails:5
from script
/rails:5:in 'require'
from script
/rails:5

At moments where several cron jobs are executed this "error(?)" is printed only once in the log.

Is there a difference between executing something from a cronjob or entering the code in the terminal? Why can there be a difference and how do I ensure that it also works from the cronjob? Also is the log output supposed to be an error, it's not telling me much unfortunately.

andreas

unread,
Apr 15, 2012, 10:04:10 PM4/15/12
to Whenever Gem
Having a similar issue. It seemed like nothing was happening, and my
task executed perfectly from the terminal.

In my log file I had the following error:
httpdocs/releases/20120415230503/config/boot.rb:1:in `require': no
such file to load -- rubygems (LoadError)
from httpdocs/releases/20120415230503/config/boot.rb:1
from script/rails:5:in `require'
from script/rails:5

I found this post: http://stackoverflow.com/questions/2896485/no-such-file-to-load-rubygems-loaderror
which suggested adding in the PATH to RubyGems in my bash profile
file. Once I did that I started getting the emails from the cron job,
but the RoR code still isn't working because now I'm getting this
error:
/usr/local/lib/ruby/1.9.1/rubygems.rb:152: uninitialized constant
Gem::Mutex (NameError)
from httpdocs/releases/20120415230503/config/boot.rb:1:in `require'
from httpdocs/releases/20120415230503/config/boot.rb:1

dipsaus...@gmail.com

unread,
Apr 18, 2012, 5:01:43 AM4/18/12
to whenev...@googlegroups.com
If ound out that I had Ruby in both /usr/lib and /usr/local/lib. I removed the first and added the following to my cron file: PATH="/usr/local/bin:/usr/bin:/bin", after that everything works for me now.

goo...@umito.nl

unread,
Dec 13, 2012, 6:24:50 PM12/13/12
to whenev...@googlegroups.com, dipsaus...@gmail.com
I have the same issue. It runs perfectly from console when I copy the cron command, but nothing happens.
I see in the syslog the mentioned cron command, but it doesn't show anything else, and it clearly doesn't run, because no data is generated. What am I missing?

Is it because --silent is enabled that I don't get any output?

goo...@umito.nl

unread,
Dec 14, 2012, 7:23:29 AM12/14/12
to whenev...@googlegroups.com, dipsaus...@gmail.com
Well, I disabled the -silence switch by redefining the job and added a log file.
/bin/bash: /usr/bin/bundle: /usr/bin/ruby1.9.1: bad interpreter: No such file or directory
Apparently it is using an other ruby, since I have it in local.

goo...@umito.nl

unread,
Dec 14, 2012, 7:28:41 AM12/14/12
to whenev...@googlegroups.com, dipsaus...@gmail.com, goo...@umito.nl
It appeared that I was using an old bundle script that referred to an unexisting ruby version. Should now work. 

goo...@umito.nl

unread,
Dec 14, 2012, 6:07:19 PM12/14/12
to whenev...@googlegroups.com, dipsaus...@gmail.com, goo...@umito.nl
Still not working due to that my ruby is in /usr/local/bin/ruby. As well as bundle.

I tried setting "env :PATH, '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'" in my schedule.rb, but that did nothing.

Any way to set the path??

Javan Makhmali

unread,
Dec 14, 2012, 6:13:38 PM12/14/12
to whenev...@googlegroups.com
Try putting /usr/local/bin/ruby at the front of your PATH so it takes precedence.

--
You received this message because you are subscribed to the Google
Groups "Whenever Gem" group and because you're awesome.
To post to this group, send email to whenev...@googlegroups.com
To unsubscribe from this group, send email to
whenever-gem...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/whenever-gem?hl=en

Reply all
Reply to author
Forward
Message has been deleted
0 new messages