designwithfocus

Back end & development

What Is a Cron Job? The Five-Field Schedule POSIX Describes

By Design With Focus Team 6 min read
A five-column schedule card beside a short terminal command

Short answer

A cron job is a command plus the times a crontab says to run it, using the five fields POSIX specifies. Those fields are minute, hour, day of month, month, and day of week. The sixth field is the command the shell runs. The spec's weekday sample, 15 3 * * 1-5, is 3:15 AM Monday through Friday.

Last verified against the sources listed below.

Key takeaways

  • POSIX defines a crontab line as six fields: five time patterns, then the command.
  • Minute is 0-59, hour is 0-23, day of month is 1-31, month is 1-12, and weekday is 0-6 with Sunday as 0.
  • The POSIX sample 15 3 * * 1-5 runs at 3:15 AM on Monday through Friday.
  • crontab -e edits, crontab -l lists, and crontab -r removes the invoking user's entry.
  • An unescaped % in the command field becomes a newline. Text after the first % is standard input.

What is a cron job? It is one scheduled command in a user’s crontab, and the POSIX.1-2017 crontab page defines the line as six fields.

The same page names the utility as a way to schedule periodic background work. That kind of timer sits in the background of back end development, next to the request handlers people notice first.

What are the five time fields?

POSIX requires five integer patterns before the command, in a fixed order, and each pattern has a closed range.

A field can be an asterisk, meaning every valid value, or a number, or two numbers with a hyphen for an inclusive range, or a comma-separated list of those elements. Blank lines are ignored. A line whose first non-blank character is # is ignored too.

Field Range in POSIX What an asterisk means
Minute 0-59 Every minute
Hour 0-23 Every hour
Day of month 1-31 Every day of the month
Month 1-12 Every month
Day of week 0-6, and 0 is Sunday Every weekday

The POSIX examples use 15 3 * * 1-5 for a cleanup that should run every weekday morning at 3:15 AM. The 1-5 range is Monday through Friday when Sunday is 0.

What does the sixth field do?

The sixth field is the command string, and sh executes it at the times the first five fields name.

POSIX says a % in that field is translated to a newline. A backslash before a character, including %, treats that character literally. Only the first line of the command field, up to a % or the end of the line, is executed by the shell.

The other lines are given to the command as standard input.

The spec’s second example is a birthday note: 0 12 14 2 * mailx john%Happy Birthday!%Time for lunch. The mail command is the text before the first percent sign. The greeting and the lunch line become the message body.

If the command does not redirect standard output and standard error, POSIX says the implementation mails that output to the user. Redirecting inside the command is how you keep a noisy job out of the mailbox.

How do -e, -l, and -r work?

crontab -e edits your entry, crontab -l prints it, and crontab -r removes it.

With -e, the utility edits a copy of the invoking user’s crontab, or creates an empty one if none exists. When editing finishes, that copy is installed. The EDITOR variable chooses the program.

If it is unset, POSIX says the default editor is vi.

-l writes the entry to standard output. -r removes it. You can also replace the entry by passing a file, or by piping a file to crontab with no option.

Exit status 0 means success. A status greater than 0 means an error, and the user’s entry is not submitted, removed, edited, or listed.

The page warns that running crontab alone reads a new entry from standard input. End of file then installs an empty entry. The interrupt character prevents that replacement.

Read the current file with -l before you replace it.

Which days match a line?

If the day of month and the day of week are both restricted, POSIX matches a day when either field matches.

The spec walks through the combinations. When month, day of month, and weekday are all asterisks, every day matches. When the month or the day of month is a list and the weekday is an asterisk, only that calendar date matches.

When the month and the day of month are asterisks and the weekday is a list, only those weekdays match.

When both a calendar date and a weekday are listed, either one is enough. The sample 0 0 1,15 * 1 runs at midnight on the 1st and the 15th, and also every Monday. The sample 0 0 * * 1 runs only on Mondays.

The Debian crontab manual says its cron only checks whether the first character is an asterisk. It gives 0 0 */2 * sun as a line that runs every Sunday that falls on an uneven date, and it says the POSIX reading would also run on every uneven date. Test that line on the cron you ship.

Where do Linux and FreeBSD differ?

The POSIX ranges above are the portable baseline. Several installed crons add names, a Sunday value of 7, and nicknames.

The Linux crontab(5) page allows weekday 0-7, with 0 or 7 as Sunday, and it allows month and weekday names. It also documents @reboot, @yearly, @annually, @monthly, @weekly, @daily, and @hourly as replacements for the five time fields. A % still becomes a newline unless you escape it.

The FreeBSD 15.1 manual, dated May 10, 2024, uses the same 0-7 weekday rule. It adds @midnight, @every_minute, and @every_second, plus -n and -q options in front of the command. Debian documents a maximum command-field length of 998 characters.

POSIX does not state that length, so treat 998 as a Debian limit.

System files such as /etc/crontab insert a username before the command. A user crontab that you edit with crontab -e does not. Copying a system line into a user file, or the reverse, shifts the command into the wrong field.

What should you check before saving?

Confirm the five ranges, the shell, and the difference between a user file and a system file before the line goes live.

POSIX supplies HOME, LOGNAME, PATH, and SHELL when the job runs. For a crontab invoked as that volume of POSIX.1-2017 specifies, SHELL is a pathname for sh. The values of those variables in the shell where you typed crontab do not change those defaults.

A command that works at your prompt can fail in the job because PATH is shorter.

The XSI option in the POSIX page describes cron.allow and cron.deny, one user name per line, in an implementation-defined directory. If neither file exists, only a process with the right privileges may submit a job. A permission error is not a bad schedule.

crontab -l shows whether the entry was installed.

Scheduled work is ordinary software development once the command is a script in the repo. Keep the script path absolute, quote it, and let the five fields stay boring.

Frequently asked questions

Does POSIX allow 7 for Sunday?

The POSIX.1-2017 crontab page limits the weekday field to 0 through 6 and says 0 is Sunday. It does not list 7. The Linux man page on man7.org, the Debian crontab manual, and the FreeBSD 15.1 manual each allow 0 through 7 and treat both 0 and 7 as Sunday. If you need a file that matches the POSIX text, use 0 for Sunday and do not use 7.

What happens if both day fields are set?

POSIX says the line matches a day if either the month and day-of-month pair matches, or the weekday matches, once both day fields are restricted. Its sample 0 0 1,15 * 1 runs on the 1st, the 15th, and every Monday. To limit the job to Mondays only, the same page sets the day of month to an asterisk: 0 0 * * 1. A line with every time field set to an asterisk matches every day.

Why did crontab erase my jobs?

POSIX application usage says a typical mistake is to type crontab with no file and no option. The utility then waits for a new entry on standard input. If you send end of file, the entry is replaced by an empty file. The page says to type the interrupt character instead, which stops the replacement. Use crontab -e when you want the editor, and crontab -l when you only want to read the current entry.

Where does command output go?

POSIX says that if the command does not redirect standard output and standard error, any output or errors are mailed to the user by an implementation-defined method. The Linux, Debian, and FreeBSD manuals add a MAILTO variable. A non-empty MAILTO sends mail to that address. An empty MAILTO sends none. Those manuals also let you set SHELL in the file. POSIX still supplies its own default HOME, LOGNAME, PATH, and SHELL when the job runs.

Are @daily and @reboot part of POSIX?

They are not in the POSIX.1-2017 field list. The man7.org page, the Debian manual, and the FreeBSD 15.1 manual document @ strings as extensions that replace the five time fields. Shared nicknames include @reboot, @yearly, @monthly, @weekly, @daily, and @hourly. Debian also lists @midnight as the same schedule as @daily. FreeBSD adds @every_minute and @every_second. A strict POSIX crontab uses the five numbers or asterisks instead of those names.

Sources