Crontab not working from whenever gem

emersonthis picture emersonthis · Nov 1, 2013 · Viewed 7.4k times · Source

My linux server is running a Rails 4 app with Nginx/unicorn.

I am using the Whenever gem to schedule tasks. Everything was working normally until a couple of days ago when the cron jobs appear to have simply stopped running.

This is in my schedule.rb file:

every 5.minutes do
    runner "Video.send_to_wistia", environment: 'production'
end

This is what I see when I run crontab -l:

# Begin Whenever generated tasks for: myapp
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /home/myuser/myapp && bin/rails runner -e production '\''Video.send_to_wistia'\'''
# End Whenever generated tasks for: myapp

I have restarted unicorn. I also made sure to run:

whenever --update-crontab myapp

In my video.rb file, which contains the action that should run, I placed some log code:

   def self.send_to_wistia
        logger.debug( "Sending videos to wistia at #{Time.now}" )
        ...

I've been watching the logs and nothing appears there.

PS: My first instinct was that maybe the site is running in development mode (which the whenever task is not configured to run in) but this does not seem to be the case, because I see now records going into my production database.

Update: As the user for my app, when I run service cron status I get this: cron: unrecognized service. But I'm not sure if this is checking the user's crontab or the system-wide.

Update2: This is what I see when I run pgrep -l cron:

410 crond
582 crond
711 crond
879 crond
1021 crond
1188 crond
1320 crond
1404 crond
1509 crond
1648 crond
1805 crond
1829 crond
1936 crond
2090 crond
2196 crond
2235 crond
2388 crond
2491 crond
2523 crond
2673 crond
2773 crond
2907 crond
2971 crond
3068 crond
3195 crond
3256 crond
3386 crond
3496 crond
3554 crond
3686 crond
3839 crond
4041 crond
4137 crond
4375 crond
4453 crond
4657 crond
4753 crond
4991 crond
5036 crond
5287 crond
5335 crond
5609 crond
5620 crond
5891 crond
5918 crond
6186 crond
6203 crond
6468 crond
6501 crond
6781 crond
6786 crond
7080 crond
7084 crond
7368 crond
7375 crond
7658 crond
7667 crond
7964 crond
7965 crond
8246 crond
8559 crond
8647 crond
8841 crond
8932 crond
9136 crond
9232 crond
9444 crond
9515 crond
9745 crond
9813 crond
10097 crond
10100 crond
10395 crond
10539 crond
10680 crond
10975 crond
11029 crond
11260 crond
11325 crond
11559 crond
11629 crond
11855 crond
11936 crond
12153 crond
12246 crond
12438 crond
12739 crond
12756 crond
13038 crond
13085 crond
13337 crond
13380 crond
13622 crond
13663 crond
13920 crond
14017 crond
14205 crond
14301 crond
14503 crond
14596 crond
14788 crond
14939 crond
15086 crond
15260 crond
15382 crond
15542 crond
15680 crond
15953 crond
15963 crond
16253 crond
16263 crond
16547 crond
16548 crond
16839 crond
16846 crond
17130 crond
17134 crond
17416 crond
17430 crond
17711 crond
17715 crond
17993 crond
18013 crond
18288 crond
18298 crond
18570 crond
18596 crond
18865 crond
18892 crond
19147 crond
19190 crond
19453 crond
19475 crond
19735 crond
19773 crond
20030 crond
20058 crond
20313 crond
20356 crond
20608 crond
20641 crond
20890 crond
20939 crond
21185 crond
21224 crond
21467 crond
21522 crond
21762 crond
21807 crond
22044 crond
22105 crond
22339 crond
22401 crond
22621 crond
22699 crond
22927 crond
22984 crond
23209 crond
23283 crond
23504 crond
23568 crond
23786 crond
23866 crond
24081 crond
24151 crond
24363 crond
24449 crond
24658 crond
24734 crond
24940 crond
25032 crond
25235 crond
25317 crond
25517 crond
25617 crond
25812 crond
25913 crond
26095 crond
26211 crond
26401 crond
26496 crond
26683 crond
26794 crond
26978 crond
27079 crond
27260 crond
27377 crond
27555 crond
27662 crond
27837 crond
27960 crond
28132 crond
28245 crond
28414 crond
28543 crond
28709 crond
28828 crond
28991 crond
29127 crond
29296 crond
29423 crond
29546 crond
29721 crond
29841 crond
30014 crond
30136 crond
30322 crond
30418 crond
30733 crond
30767 crond
31015 crond
31071 crond
31314 crond
31358 crond
31596 crond
31692 crond
31892 crond
31983 crond
32174 crond
32283 crond
32469 crond
32568 crond
32751 crond

Update 3: I ran ps -ef | grep cron and I get:

root      1404     1  0 Oct31 ?        00:00:00 crond
myappuser    3961  2746  0 10:56 pts/1    00:00:00 grep cron

Update 4: I ran /bin/bash -l -c 'cd /home/clippo/clippo2 && bin/rails runner -e production '\''Video.send_to_wistia'\''' and this is what I see:

/home/myuser/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/runner.rb:53:in `eval': No such file or directory - /home/myuser/myapp/tmp/video_uploads/190 (Errno::ENOENT)
    from /home/myuser/myapp/app/models/video.rb:31:in `new'

This error message is really confusing because that directory DOES exist and always has.

Answer

安杰帅 picture 安杰帅 · Nov 1, 2013

Does your

crontab -l

output match what you have in your schedule file for your whenever gem?

I usually do

whenever -w

any time I make changes to my schedule file and want my crontab updated.

Although it seems like you're having issues getting your actual rb file to run that logger command. you could try editing your schedule.rb to have a set :output like this ->

set :output, "/Users/home/rubylogs/vendorcatalog.log"

every :day, :at => "2:31am" do

command "cd /Users/home/automation/Vendor\ Catalog; parallel_test -n 1 *.rb"

end

The parallel test stuff isn't really relevant but you get the point. See if you get any output when you do it that way? That should tell you whether or not it's running something.