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: end a cron line with >> file 2>&1 to keep all output in one log

Cron
~/tips/tip-cron-append-output-to-log

*/10 * * * * /usr/local/bin/sync.sh >> /var/log/sync.log 2>&1

Sends both normal output and errors of a scheduled job to a log file you can read later.

Without a redirect, cron mails the output of every job, or drops it when no mail system is installed.

*/10 * * * * /usr/local/bin/sync.sh >> /var/log/sync.log 2>&1

Redirect standard output to the file first, then point standard error at standard output. The order matters: 2>&1 >> file sends errors to the old standard output instead of the file. >> appends, while > empties the log at the start of every run. The user the job runs as must be allowed to write to the log path, so a file under the home directory is easier than one in /var/log. Add a logrotate entry or the file grows without limit.

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