12.6. Configuring cron(8)
Prev | Chapter 12. Configuration and Tuning | Next

## 12.6. Configuring cron(8)

Contributed by Tom Rhodes.

One of the most useful utilities in FreeBSD is cron(8). This utility runs in the background and regularly checks `/etc/crontab` for tasks to execute and searches `/var/cron/tabs` for custom crontab(5) files. These files store information about specific functions which cron(8) is supposed to perform at certain times.

Two different types of configuration files are used by cron(8): the system `crontab` and user `crontab`s. These formats only differ in the sixth field and later. In the system `crontab`, cron(8) runs the command as the user specified in the sixth field. In a user `crontab`, all commands run as the user who created the `crontab`, so the sixth field is the last field; this is an important security feature. The final field is always the command to run.

### Note:

User crontabs allow individual users to schedule tasks without the need for `root` privileges. Commands in a user's crontab run with the permissions of the user who owns the crontab.

The `root` user can have a user `crontab` just like any other user. The `root` user `crontab` is separate from the system `crontab`, `/etc/crontab`. Because the system `crontab` invokes the specified commands as `root`, there is usually no need to create a user `crontab` for `root`.

Here is a sample entry from `/etc/crontab`:

# /etc/crontab - root's crontab for FreeBSD # # $FreeBSD: head/en_US.ISO8859-1/books/handbook/config/chapter.xml 42014 2013-06-23 22:37:08Z gjb $ # # SHELL=/bin/sh PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin # #minute hour mday month wday who command # */5 * * * * root /usr/libexec/atrun

| Like most FreeBSD configuration files, lines that begin with the `#` character are comments. A comment can be placed in the file as a reminder of what and why a desired action is performed. Comments cannot be on the same line as a command or else they will be interpreted as part of the command; they must be on a new line. Blank lines are ignored.
| The equals (`=`) character is used to define any environment settings. In this example, it is used to define the `SHELL` and `PATH`. If the `SHELL` is omitted, cron(8) will use the default of sh(1). If the `PATH` is omitted, no default will be used and file locations will need to be absolute.

This line defines a total of seven fields: `minute`, `hour`, `mday`, `month`, `wday`, `who`, and `command`. These are almost all self explanatory. `minute` is the time in minutes when the specified command will be run. `hour` is the hour when the specified command will be run. `mday` stands for day of the month and `month` designates the month. The `wday` option stands for day of the week. These fields must be numeric values, representing the twenty-four hour clock, or a `*`, representing all values for that field. The `who` field only exists in the system crontab. This field specifies which user the command should be run as. The last field is the command to be executed.

This last line defines the values discussed above. This example has a `*/5` listing,followed by several more `*` characters. These `*` characters mean “first-last”, and can be interpreted as every time. In this example, atrun(8) is invoked by `root` every five minutes, regardless of the day or month.

Commands can have any number of flags passed to them; however, commands which extend to multiple lines need to be broken with the backslash “\” continuation character.

This is the basic setup for every crontab(5). However, field number six, which specifies the username, only exists in the system crontab(5). This field should be omitted for individual user crontab(5) files.

### 12.6.1. Installing a Crontab

### Important:

Do not use the procedure described here to edit and install the system `crontab`, `/etc/crontab`. Instead, use an editor and cron(8) will notice that the file has changed and immediately begin using the updated version. See this FAQ entry for more information.

To install a freshly written user crontab(5), use an editor to create and save a file in the proper format. Then, specify the file name with crontab(1):

`%` `crontab crontab-file`

In this example, `crontab-file` is the filename of a crontab(5) that was previously created.

To list installed crontab(5) files, pass `-l` to crontab(1).

Users who wish to begin their own `crontab` file from scratch, without the use of a template, can use `crontab -e`. This will invoke the default editor with an empty file. When this file is saved, it will be automatically installed by crontab(1).

In order to remove a user crontab(5) completely, use `crontab -r`.

Prev | Up | Next
12.5. Starting Services | Home | 12.7. Using rc(8) Under FreeBSD

All FreeBSD documents are available for download at http://ftp.FreeBSD.org/pub/FreeBSD/doc/

Questions that are not answered by the documentation may be sent to <freebsd-questions@FreeBSD.org>.
Send questions about this document to <freebsd-doc@FreeBSD.org>.