Skip to content

Cron

Crontab expressions

If you ever come to the point where you can't remember what exactly some cron expression mean there is a tool Crontab Guru to demystify them.

Check crons set up for single user

    crontab -l -u 'USER';

Check crons set up for all users

    cut -f 1 -d ':' /etc/passwd | while read f; 
    do 
        echo "crontab for $f"; 
        crontab -l -u $f; 
    done

Debugging cron scripts that fail

Sometimes you will write a script that will work perfectly when you run it, but it when it runs with cron something goes wrong.

This can be caused by cron running using a different environment, and be incredibly difficult to debug.

To get around this, you need to replicate the environment that cron will run the script in, and see if you can then replicate the issue.

To do that first add the following line to the crontab of the user that will run the script

* * * * * env > /tmp/env

Wait until the command is run and then remove it from the cron tab. You then need to set up a shell that will use those values

env - `cat /tmp/env` /bin/sh

Note

This assumes that cron is running using /bin/sh. Confirm that by checking the SHELL var in the file that has just been created

After running the command you will be put into a shell that is identical to the one that cron will use. Try running your script and see if you can replicate the issue. Pay attention to the PATH var as this can often be different from the one that you expect