How to debug a cron problem not executing a given script - or another?

I have a Rails script that I would like to run daily. I know that there are many approaches, and that the cron'd script/runner approach is not favored by some, but seems to meet my needs.

However, my script is not executing as planned.

My application runs in /data/myapp/current , and the script script/myscript.rb in script/myscript.rb . I can start it manually without problems as root with:

 /data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb 

When I do this, a special log/myscript.log file ( log/myscript.log ) is logged as expected:

 Tue Mar 03 13:16:00 -0500 2009 Starting to execute script... ... Tue Mar 03 13:19:08 -0500 2009 Finished executing script in 188.075028 seconds 

I am going to work with cron every morning at 4 a.m. root crontab:

 $ crontab -l 0 4 * * * /data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb 

In fact, it looks like he was trying to log in as recently as this morning!

 $ tail -100 /var/log/cron ... Mar 2 04:00:01 hostname crond[8894]: (root) CMD (/data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb) ... Mar 3 04:00:01 hostname crond[22398]: (root) CMD (/data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb) ... 

However, there is no entry in my log file, and the data it should update was not updated. Log file permissions (as a test) were even configured to write globally:

 $ ls -lh total 19M ... -rw-rw-rw- 1 myuser apps 7.4K Mar 3 13:19 myscript.log ... 

I work on CentOS 5.

So my questions are ...

  • Where else can I look for information to debug this?
  • Could this be a SELinux problem? Is there a security context that I could set or change that could solve this error?

Thanks!

Update

Thanks to Pavel and Luke. This turned out to be an environmental problem, and capturing stderr in a log file allowed me to find the error.

 $ cat cron.log /usr/bin/env: ruby: No such file or directory $ head /data/myapp/current/script/runner #!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/runner' 

Adding the Ruby executable to the command did the trick:

 $ crontab -l 0 4 * * * /usr/local/bin/ruby /data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb >> /data/myapp/current/log/cron.log 2>&1 
+4
source share
3 answers

By default, cron sends its output to the user who ran it. You can look there.

It is very useful to redirect the output of scripts executed by cron so that you can look at the results in the log file, and not at random local mail of the user on the server.

Here you can redirect stdout and stderr to a log file:

 cd /home/deploy/your_app/current; script/runner -e production ./script/my_cron_job.rb >> /home/deploy/your_app/current/log/my_file.log 2>&1 

>> redirect stdout to a file, and 2>&1 redirects stderr to stdout so that any error messages are also logged.

By doing this, you will be able to view error messages to see what is actually happening.

+7
source

A common problem when someone discovers that their script will not be executed in the cron job when it is run from the command line is that it relies on some part of the environment that the interactive session has, but the cron does not receive. candidates are the PATH environment, and possibly HOME.

+1
source

On Linux, make sure that all configuration files (/etc/crontab,/etc/crond.{daily,hourly,etcasket/* and /etc/cron.d/*) are accessible only to the root user and are not symbolic links, otherwise they will not even be considered.

To allow non-root and / or symbolic links, specify the -p option to the crond daemon.

-1
source

All Articles