2

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?

kenny99
  • 259
  • 6
  • 19
  • can you give more detail about what's going wrong ? (e.g. error messages etc.) – Andre Holzner Aug 31 '10 at 18:04
  • 1
    Asking us to find a bug when you show neither the code nor the error message is bound to end in disappointment. See http://www.catb.org/esr/faqs/smart-questions.html – Bryan Oakley Aug 31 '10 at 18:25
  • Fair comment Bryan, apologies for the vagueness of the post. Will update original post with more info. – kenny99 Aug 31 '10 at 18:56

3 Answers3

18

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

Next to check are permissions. Which user is the cron job run as? Does it 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!

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • 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 Aug 31 '10 at 18:58
2

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
  • 46,381
  • 14
  • 112
  • 137
0

For me, the problem was a different shell interpreter in crontab.

Vanuan
  • 31,770
  • 10
  • 98
  • 102