brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a % in a cron command must be written as \% or the line breaks

Cron
~/tips/tip-cron-escape-percent-sign

0 3 * * * tar czf /backup/home-$(date +%F).tgz /home

Explains why a percent sign inside a crontab line cuts the command short and how to escape it.

A date in a backup filename is the usual reason a cron line works in the terminal and fails from the crontab.

# breaks: cron turns each % into a newline
0 3 * * * tar czf /backup/home-$(date +%F).tgz /home
# works
0 3 * * * tar czf /backup/home-$(date +\%F).tgz /home

In a crontab an unescaped % ends the command, and everything after it is fed to the command as standard input. The first line above therefore runs tar czf /backup/home-$(date + and dies with a syntax error that cron mails to the owner. Put a backslash in front of every %. The rule only applies inside crontab files, so a command moved into a script can use date +%F as usual. That is the other way out when a line gets full of backslashes.

Further reading: crontab(5) manual page

Going deeper: Guide: find out why a Linux service failed, with journalctl

More Cron tips echo 'systemctl restart myapp' | at 03:00 tomorrow 55 23 28-31 * * [ "$(date -d tomorrow +\%d)" = 01 ] && /usr/local/bin/month-end.sh EDITOR=nano crontab -e 30 6 * * mon /usr/local/bin/weekly-report.sh @daily /usr/local/bin/backup.sh All Cron tips (32) All topics

Link