Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crontab not working from whenever gem

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.

like image 639
emersonthis Avatar asked Nov 01 '13 14:11

emersonthis


People also ask

How often does a cron job run?

Anatomy of a Crontab Entry Cron will execute the command when the minute, hour, month, and either day of month or day of week match the current time. The preceding cron job will run once per day every five days, from 5th to 20th of February plus all Tuesdays of February.

Can two cron jobs run simultaneously?

Technically it's possible to run both imports at the same time via cron jobs, but it's not recommended – actively running imports are resource-intensive and could cause your server to struggle.

What is the name of the special file that the cron program looks for when it is scheduling events?

The actions of cron are driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule. The crontab files are stored where the lists of jobs and other instructions to the cron daemon are kept.

How do I create multiple cron jobs in Linux?

Creating Cron JobsThe first executes a backup script. The second will create a new file and the last will rename the created file if it exists. We will schedule the tasks to run at different times but on the same day using the commands below. As shown, we've added the cron jobs at the bottom of the crontab file.


3 Answers

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.

like image 172
安杰帅 Avatar answered Sep 21 '22 02:09

安杰帅


This turned out to be a wild goose chase. The cron probably WAS working all along, but the script its self was erroring out (thanks to @Arsen7 for pointing me in this direction), giving the appearance that it had stopped. If you look closely at the error message under Update 4 above, you'll see...

/home/myuser/myapp/tmp/video_uploads/190

I initially missed the /190, which should be a file name (190.MOV). But it was missing an extension and appearing to be a directory, which does not exist, and caused the whole script to fail.

like image 34
emersonthis Avatar answered Sep 22 '22 02:09

emersonthis


As a first guess, it could be that your cron daemon has stopped working.

It depends on what type of cron is installed on your system, but I guess that the command pgrep -l cron should list some processes that have "cron" in its names.

Another thing could be to check the system log - usually found in /var/log/messages or nearby. Maybe there are some lines with "cron" in it? A grep -i cron /var/log/messages could be helpful, but it depends on what kind of system logger you have - so the logfile could be different - and how often it is rotated (you have said that the cron stopped working a few days ago).

UPDATE

It looks that your crond is running - even if it has a lot of instances, what might be or might be not unusual.

Now you could follow the fedorqui's advice, and confirm that the line used in the crontab file really works. Maybe it hangs, and thus no new invocations of the cron are created?

If the command works properly, you may want to see the logfiles - either the system log message (something under /var/log directory) or the cron's own logfile (maybe your cron daemon is configured to output its messages into - for example - /var/log/crond).

Usually the cron daemon outputs a log message whenever it runs a task. It also logs when you see or edit the crontab file. These messages may be useful.

If you find no information in the logfiles, then you could just try to restart the cron service. I cannot tell you how to do it, because it depends on the configuration of your server.

like image 30
Arsen7 Avatar answered Sep 21 '22 02:09

Arsen7