Skip to content
← Blog

One % sign can break a cron job every night

A cron line can look right and still fail every run. The crontab man page says an unescaped % in the command is changed into a newline, and everything after the first % is sent to the command as standard input. (crontab(5) man page)

So this backup line works when you paste it into a terminal:

bash
0 2 * * * tar czf /backup/site-$(date +%F).tgz /var/www

In a crontab, the shell only gets tar czf /backup/site-$(date +, which is a broken command.

The fix: write \% in crontab lines, or move the command into a script. Then add a heartbeat after it with &&, so a run that errors never checks in and you get an alert.

Set up a cron heartbeat, free for 3 watches.