noted.do
The Tech Guy

Cron, the Tiny Scheduler Running Half Your Server

Somewhere on almost every Linux server there's a little program that wakes up once a minute, checks a list, and runs whatever is due. That's cron. It's been doing this job since before most of us were born, and it's still how a huge amount of routine work gets done: nightly backups, clearing out temp files, renewing certificates, sending the daily report nobody reads.

A "cron job" is just one line in that list. The list is called a crontab (cron table), and you edit yours with:

crontab -e

Each line is a schedule followed by a command. The schedule is five fields, and this is the part everyone has to look up:

┌───────── minute (0-59)
│ ┌─────── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌─── month (1-12)
│ │ │ │ ┌─ day of week (0-6, Sunday is 0)
│ │ │ │ │
30 2 * * * /home/you/scripts/backup.sh

That one runs at 2:30 every morning. A * means "any," so it doesn't care what day or month it is. A few more so the pattern sticks:

*/15 * * * *   every 15 minutes
0 9 * * 1-5    9:00 on weekdays
0 0 1 * *      midnight on the first of each month
0 */6 * * *    every six hours, on the hour

Many systems also accept shortcuts like @daily, @hourly and @reboot, which are easier to read if you don't need anything fancy. And if you can never remember which field is which (I can't, reliably), crontab.guru will translate any schedule into plain English for you.

That's the easy part. The reason cron has a reputation for being annoying is that jobs which work perfectly when you run them by hand quietly fail when cron runs them. Almost always for one of the same few reasons.

The big one is the environment. When you type a command in your terminal, you've got your full PATH, your shell config, your working directory. Cron gives you almost none of that. It runs with a bare-bones PATH and starts in your home directory. So a script that calls php or node or docker might work for you and then fail under cron with "command not found." The fix is boring: use full paths everywhere. /usr/bin/php, not php. You can find the full path with which php. You can also set PATH= at the top of the crontab, which is fine too.

The second one is output. By default cron tries to email whatever your job prints to the local user, and on most servers there's no mail setup, so it vanishes. That means when your job breaks, you get no error and no clue. Redirect output to a log file instead:

30 2 * * * /home/you/scripts/backup.sh >> /home/you/logs/backup.log 2>&1

The 2>&1 sends errors to the same file as normal output. Without it, you'd only log the good news.

A couple of smaller gotchas. The % character means something special in a crontab (it's treated as a newline), so if your command has one, like a date +%F for a filename, escape it as \% or move the command into a script. And cron uses the server's time zone, which is often UTC on a VPS. If your "9 a.m." job fires at a weird hour, that's why.

One genuinely confusing rule: if you fill in both day-of-month and day-of-week, cron runs the job when either matches, not when both do. So 0 0 13 * 5 runs on every 13th and also every Friday, not only on Friday the 13th. Surprises people every time.

There's also the question of jobs overlapping. If something runs every five minutes but sometimes takes seven, you can end up with two copies stepping on each other. flock handles this nicely by refusing to start a second copy while the first holds a lock:

*/5 * * * * /usr/bin/flock -n /tmp/sync.lock /home/you/scripts/sync.sh

Is cron still the right tool? For a lot of things, yes. On systemd-based distros, systemd timers are the newer option and they do have real advantages: logs go to the journal automatically, you can catch up on runs missed while the machine was off, and dependencies are easier to express. They're also more verbose to set up. For a simple "run this script every night" job on a server you manage, I still reach for a single crontab line.

My one piece of advice: after you add a job, don't trust it until you've seen it actually run on schedule and checked the log. Set it to run a couple of minutes from now, watch it work, then change it to the real time.

Comments

Be kind. See the rules.