Crontab Examples You Can Reuse: Backups, Logs, and Cleanups

Most crontabs are built from the same handful of jobs: a backup, a log cleanup, a health check, a report. The lines below are the ones worth copying. Each comes with the detail that decides whether it works in a month or breaks quietly on day two.

One habit before any of them: put your paths at the top of the crontab.

SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
[email protected]

Cron does not read your shell profile. A script that works in your terminal can fail at 03:00 because rsync or python3 is not on cron’s short default path.

1. A nightly backup, with a lock

30 2 * * * flock -n /var/lock/backup.lock /usr/local/bin/backup-db.sh >> /var/log/backup.log 2>&1

Ran at 02:30 every night. The flock -n refuses a second start while the first is still running — the fix for the classic overlap, where a slow backup at 02:30 is still going when the next one begins and two jobs write to the same dump file.

The redirect sends both normal output and errors to a log. Without it, cron tries to email the output; if the server has no mail transfer agent, the text disappears.

2. A cleanup that cannot eat the wrong folder

15 4 * * 0 find /var/log/app -type f -name '*.log' -mtime +14 -delete

Every Sunday at 04:15, delete app logs older than fourteen days. Three details make this safe:

  • -type f keeps it to files, so a directory whose name ends in .log is never touched.
  • The path is absolute and narrow. A stray space in find arguments can turn a cleanup into a wipe.
  • -mtime +14 means strictly more than fourteen full days, so a log written today is never a candidate.

Run the same find once without -delete and read the list first. It costs ten seconds and prevents the one mistake you cannot undo.

3. A health check that says something

*/5 * * * * curl -fsS --max-time 10 https://example.com/health > /dev/null || /usr/local/bin/alert.sh "health check failed"

Every five minutes, ask the site whether it is healthy. -f makes curl fail on an HTTP error instead of printing the error page, -sS hides the progress bar but keeps the message, and --max-time stops a hanging request from stacking up.

The || is the whole point. A check that only writes to a log is a check nobody reads.

4. A monthly report on the first

0 7 1 * * /usr/local/bin/monthly-report.sh

At 07:00 on the first of each month. This is the rollover shape from month-end jobs: running on the first avoids every short-month problem, because the first exists in every month.

If the report depends on last month’s data, have the script work on “the previous calendar month” rather than on “yesterday”.

5. A mirror that keeps permissions

0 3 * * * rsync -a --delete /srv/data/ /mnt/backup/data/ >> /var/log/rsync.log 2>&1

Nightly at 03:00. -a keeps permissions, timestamps and symlinks; --delete removes files at the destination that no longer exist at the source, which is what makes it a mirror rather than an ever-growing pile.

--delete is also the most dangerous flag in this list. Keep the trailing slash on the source path — /srv/data/ means “the contents of data”, while /srv/data means “the folder itself”. One character changes the layout of the destination.

6. A renewal twice a day

17 3,15 * * * certbot renew --quiet --deploy-hook "systemctl reload nginx"

At 03:17 and 15:17. The odd minute is deliberate: renewals are spread across the fleet, and a round number means every server on earth asks at the same second. Certbot only renews certificates that are close to expiry, so most runs do nothing.

The four traps behind every line

Most broken crontab entries fail for one of four reasons, and none of them is the schedule:

  • Path. Cron’s PATH is short. Use absolute paths in the line, or set PATH at the top of the crontab.
  • Environment. No HOME, no locale, no shell profile. A script that reads ~/.config will look in the wrong place.
  • Output. Cron mails it and forgets it. Redirect to a file, then read the file — or set MAILTO and keep an inbox that is monitored.
  • Percent signs. An unescaped % becomes a newline inside the command. Anything that formats a date needs \%.

Copy, then check the clock

Every line above is plain five-field cron, so it works in a system crontab, a user crontab and most hosting panels. Before you paste one, confirm two things: the server’s timezone (cron follows the system clock, so a job set for 02:30 runs at 02:30 there, not where you sit), and that the command works when run by hand with an empty environment.

The schedule library has the exact expression for the common cases with the trap each one hides, the cheat sheet keeps the syntax on one page, and the mistakes page covers the failures that are not about the schedule at all.