tips
32 tips Cron
One command, setting or habit at a time about Cron: what it does, the line to copy and a caveat or two.
Syntax 10
-
Runs a cron job on the last day of every month, whatever its length.
55 23 28-31 * * [ "$(date -d tomorrow +\%d)" = 01 ] && /usr/local/bin/month-end.sh -
Makes crontab lines easier to read with day and month names, and explains the two Sundays.
30 6 * * mon /usr/local/bin/weekly-report.sh -
Uses readable shortcuts like @daily instead of five numbers for common cron schedules.
@daily /usr/local/bin/backup.sh -
Combines a range and a step in one cron field, for schedules limited to office hours.
0 9-17/2 * * 1-5 /usr/local/bin/sync-orders.sh -
Shows why a comment after a crontab command or variable gets treated as part of that line.
[email protected] # alerts go here -
Lets a cronie crontab pick a random minute in a range so many machines do not start at once.
6~15 3 * * * /usr/local/bin/sync-things.sh -
Explains why jobs at 02:30 can be skipped or run twice when the clocks change, and how to avoid it.
30 2 * * * /usr/local/bin/nightly-backup.sh -
Warns that cron may treat a crontab as broken when its last line has no newline at the end.
printf '0 3 * * * /usr/local/bin/backup.sh' | crontab - -
Explains why a cron step that does not divide 60 gives one short gap every hour.
*/15 * * * * /usr/local/bin/poll.sh # 15 divides 60, so every gap is 15 minutes -
Shows that restricting both day fields in a cron schedule widens it instead of narrowing it.
0 9 13 * 5 /usr/local/bin/report.sh
Environment and logs 5
-
Captures the exact environment cron gives your jobs, so you can run a script the same way by hand.
* * * * * env > /tmp/cron-env.txt 2>&1 -
Hides the output of cron jobs that succeed, so the mails you get always mean something.
0 3 * * * chronic /usr/local/bin/backup.sh -
Lets a crontab use local Dutch time even when the server clock runs on UTC.
CRON_TZ=Europe/Amsterdam -
Lists the log lines that prove whether cron started a given job and at what time.
journalctl -u cron --since today -
Turns off or redirects the mail cron sends with the output of each job.
MAILTO=""
Scheduled tasks 5
-
Picks the editor for crontab -e for one command or for good.
EDITOR=nano crontab -e -
Explains how installing a crontab from a file works, and how to add a line without losing the rest.
crontab cron.txt -
Runs a script once at every boot, straight from your crontab.
crontab -e -
Turns off WordPress's visitor-triggered scheduler so a real server cron job runs the scheduled tasks instead.
define( 'DISABLE_WP_CRON', true ); -
Runs every overdue WordPress scheduled task immediately instead of waiting for the next visitor.
wp cron event run --due-now
Alternatives 3
-
Schedules a single command for later without adding a cron line you have to remember to delete.
echo 'systemctl restart myapp' | at 03:00 tomorrow -
Makes a systemd timer run a missed job right after boot.
Persistent=true -
Runs periodic jobs after boot when the computer was off at their normal time.
anacron -T
System 3
-
Lists the personal crontabs of every account, to find a job that runs from somewhere unexpected.
sudo ls -l /var/spool/cron/crontabs -
Limits which user accounts are allowed to create and edit their own cron jobs.
printf 'root\nbackup\n' | sudo tee /etc/cron.allow -
Lists which scripts in a cron directory would be executed without running them.
run-parts --test /etc/cron.daily
Panes 2
-
Sends both normal output and errors of a scheduled job to a log file you can read later.
*/10 * * * * /usr/local/bin/sync.sh >> /var/log/sync.log 2>&1 -
Explains why a percent sign inside a crontab line cuts the command short and how to escape it.
0 3 * * * tar czf /backup/home-$(date +%F).tgz /home
Copy mode and scrollback 1
-
Skips a scheduled run when the previous one still holds a lock file.
*/5 * * * * flock -n /run/lock/sync.lock /usr/local/bin/sync.sh
Inspect and logs 1
-
Saves your crontab to a file and makes crontab ask before it removes everything.
crontab -l > ~/crontab.backup
Logs and diagnostics 1
-
Gives every job in a crontab the same PATH and shell so commands are found like in a terminal.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
System jobs 1
-
Explains the extra user column that system crontab files in /etc/cron.d require.
17 2 * * * root /usr/local/bin/backup.sh
Nothing in this filter yet.