views:

37

answers:

2

Hi,

I'm struggling trying to debug a cron job which isn't working correctly. The cron job calls a shell script which should unrar a rar file - this works correctly when i run the script manually, but for some reason it's not working via cron. I am using the absolute file path and have verified that the path is correct. Has anyone got any ideas why this could be happening?

+2  A: 

The first thing to check when cron jobs fail is to see if the full environment is available to the script you are trying to execute. In other words, you need to realize that a job executed via cron runs as a detached process meaning it is not associated with a login environment. Therefore whenever you try to debug a cron job that works when you execute manually, you need to be sure the same environment is available to the cronjob as is available to you when you execute it manually. This include any PATH settings, and other envvars that the script may depend on.

ennuikiller
A: 

Well, you already said that you have used absolute paths, so the number 1 problem is dealed with.

Next to check are permissions. As which user is the cron job run ? Does he have all the permissions necessary ?

Then, a little trick: if you have a shell script that fails and it's not run in a terminal I like to redirect the output of it to some files. Right at the start of the script, add:

exec &>/tmp/my.log

This will redirect STDOUT and STDERR to /tmp/my.log. Then it might also be a good idea to also add the line:

set -x

This will make bash print which command it's about to execute, and at what nesting level.

Happy debugging !

DarkDust
Thanks for the tip - this helped to show that the unrar script wasn't the problem, it's the preceeding cron (a php script) which is supposed to download a rar file for the unrar script to process.
kenny99