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: set PATH at the top of a crontab, because cron jobs do not get your login PATH

Cron
~/tips/tip-cron-set-path-in-crontab

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Gives every job in a crontab the same PATH and shell so commands are found like in a terminal.

“command not found” from a job that runs fine in a terminal almost always means cron has a different environment.

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SHELL=/bin/bash
0 * * * * backup-tool --quiet

Cron does not read your shell profile. Jobs start with a tiny environment: the shell is /bin/sh and on Debian PATH is only /usr/bin:/bin. Variable assignments at the top of a crontab apply to every job below them, so one line fixes all of them. SHELL=/bin/bash also makes bashisms like [[ ]] work. Write the full value out and do not rely on PATH=$PATH:/opt/bin, since the man page does not promise any variable expansion in these lines. Absolute paths in the command itself are the other option.

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