Cron Cheatsheet

Apr 12, 2020 · 601 words · 3 minute read

Cron is a way of scheduling tasks to run at specific times and intervals on *nix type system. I don't do it super often, so I tend to forget some of the details.

There are three main parts to 'cron':

  • cron(d) - the program that runs every minute and checks all installed crontab files
  • crontab - the configuration file containing the schedules, and commands to run, each being called a,
  • cron job - the specific scheduled task.

Interacting with Cron

If your crontab file already exists, you can see the contents with:

crontab -l

To edit (or create) a crontab use

crontab -e

This will default to whatever you value of $EDITOR is, probably vi(m).

Crontab Schedule

A cron job schedule is defined with 5 positions, and the job is triggered when all 5 positions are true. A * means 'any'. The positions are:

DefinesMinuteHourDay of MonthMonthDay of week
Values0-590-231-311-120-6(7)

Day of the week starts on a Sunday (why??), but on some implementations 0 or 7 can be Sunday, so easiest to remember that Monday is 1.

The values can also include:

  • Ranges: .e.g 1-4 would include hours 1, 2, 3 and 4.
  • Lists: e.g. days 1,3,5 would be Monday, Wednesday and Friday
  • Steps: /<number> divides the value and repeats the command then. You can use any of the above methods, including a star. e.g. */2 in the hours field would mean every two hours.

For FreeBSD's cron you can also use certain 'special' values related to periodic tasks that start with an @. Most can be replicated in the standard format (see the man page), but additionally useful ones are @reboot which runs once when cron starts, and @every_second which is a frequency you otherwise can't specify. (Not that I can think of anything you want to run that often.)

Commands and Job Outputs

After the five schedule positions comes the command you want to run, often a script. The default location to start from is the user's $HOME directory, but especially if you're running a script with sudo (or doas) it's best to be explicit and not give relative paths. (If it's a script, make sure you cd into the directory you want to be in to be certain.)

If you're not sure you program is on the $PATH, either update that (see options below), or again, give the absolute path.

The default is the mail (locally) the job's output (both stdout and stderr). If you want only to receive a mail when there's a failure, then add >/dev/null after the job command to send stdout to /dev/null, stderr will still be sent. This is probably mostly what you want.

To fully silence a job at 2>&1 after the command, and it'll dispose of both stdout and stderr.

Crontab Options

You can add name = value options to the start of the crontab file, the most useful are:

  • SHELL - this is the shell that's used to run the commands, can be useful to specify /bin/sh vs bash.
  • PATH - Might have to extend the usual path if a program you're looking for can't be found
  • MAILTO - Normally cron sends the output of cron jobs to the user's local mail address. Here you can specify another address (external if a mail agent is set up). If this is set to "" (nothing), then no mails are sent anywhere.

Useful Links

Updated 2026 to add FreeBSD specific notes