Blog Tags: 

How to debug a broken cron job

I just fixed a broken cron job. It turned out it was a PATH issue. By default cron doesn't have /usr/local stuff in the PATH.

Tips on how to debug cron:

  • change the schedule for the cron job so it runs every minute. It's harder to debug something that happens infrequently.

  • Make sure syslog sends cron logs to /var/log/cron.log. On one of our servers this line was commented out for some reason:

Unix buffering delays output to stdout, ruins your day

Let's say you have the following program:

cat>example.py<<'EOF'
#!/usr/bin/python
import time
while True:
    print 'hello world'
    time.sleep(1)
EOF

chmod +x ./example.py

If you run this program from a terminal, it will print hello world every second.

But redirect the output to a file and something different happens:

./example.py > output &
tail -f output

You won't see any output! (At least not for a long while)

The same is true if you redirect example.py's through a unix pipe which you can do on the shell: