Replace Crontab with systemd Timers for WordPress Maintenance Tasks

systemd timers instead of crontab for WordPress maintenance

Cron runs a command at a time and forgets about it: the output goes to a mail nobody reads, a failure goes unnoticed, and a job that runs longer than its interval starts a second time. A systemd timer starts a service unit instead, so each run is logged in the journal with its exit status, never overlaps with the previous one, and can trigger an alert when it fails.

Problem: The crontab of a WordPress server has seven jobs, and nobody knows whether last night’s backup ran, how long it took or why it failed last week.

Solution: For each job, write a .service unit (what to run, as which user) and a .timer unit (when), enable the timer, read the runs with journalctl, and add OnFailure= in the [Unit] section to start a notification unit when a run fails.

WP-Cron every five minutes

# /etc/systemd/system/wp-cron.service
[Unit]
Description=Run due WP-Cron events for example.com
OnFailure=notify-failure@%n.service

[Service]
Type=oneshot
User=www-data
ExecStart=/usr/local/bin/wp --path=/var/www/example cron event run --due-now --quiet
# /etc/systemd/system/wp-cron.timer
[Unit]
Description=WP-Cron every five minutes

[Timer]
OnBootSec=2min
OnUnitActiveSec=5min
AccuracySec=30s

[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now wp-cron.timer
systemctl list-timers wp-cron.timer

OnUnitActiveSec counts from the last start, and a oneshot service that is still running is not started again, so runs never overlap. Disable the page-load trigger in WordPress with define( 'DISABLE_WP_CRON', true ); as with cron.

Nightly backup at a fixed time

# /etc/systemd/system/wp-backup.timer
[Timer]
OnCalendar=*-*-* 02:00:00
RandomizedDelaySec=15min
Persistent=true

[Install]
WantedBy=timers.target

The matching wp-backup.service runs the backup script. Persistent=true runs a missed job after the server was off at 02:00. RandomizedDelaySec spreads the start, useful when many servers back up to the same storage. systemd-analyze calendar "*-*-* 02:00:00" shows when an expression fires next.

The failure alert

# /etc/systemd/system/notify-failure@.service
[Unit]
Description=Mail about failed %i

[Service]
Type=oneshot
ExecStart=/bin/sh -c 'systemctl status --no-pager "%i" | mail -s "[FAIL] %i on %H" admin@example.com'

%i is the name of the failed unit, %H the host name. OnFailure= belongs in the [Unit] section of the job’s service; placed in [Service], systemd ignores it with a warning.

Read the history

journalctl -u wp-backup.service --since yesterday
systemctl status wp-backup.service
sudo systemctl start wp-backup.service    # run it now, by hand

The same jobs with plain cron, for comparison: Linux cron jobs for WordPress.

NOTE: Units in /etc/systemd/system/ need root to install, which makes them a tool for servers you manage yourself; on shared hosting, cron is what you get. Keep the commands in the service units short and put the logic into scripts, so the same script also runs by hand.