How to use crontab command in FreeBSD
Crontab is a program for installing, uninstalling and reading tables and used to control the daemon cron, standard task scheduler in Unix systems. Each user has its own crontab. Keep in mind that they are located in /var, and are not intended for direct editing.
Crontab files and directories
This command works with two files:
- /var/cron/allow - list of users which are allowed to use crontab;
- /var/cron/deny - list of users which are prohibited from using crontab.
If the allow file exists, then users that are going to use crontab must be listed there. The same applies to deny file, but the other way around, users listed in it won't be allowed to use crontab.
The format of these files is one user per line without leading or trailing whitespace.
For example to define a list of users which are allowed to use crontab, the file /var/cron/allow has to be created with the following content:
charlie robert donald
Where: charlie, robert, donald are users in the system.
If neither of these files exists, then only a super user or every current user will be allowed to use crontab, it depends on system configuration parameters.
After working with crontab, the configuration will be always stored in /var/cron/tabs directory in the file with the name of the user whose crontab configuration was created or edited.
Crontab has the following options:
- -u user - specifies the name of the user whose crontab has to be selected to work with. Usually applied with other options.
- -l - shows the current crontab of the current user.
- -r - deletes the current crontab
- -e - edits the current crontab by the editor specified in VISUAL or EDITOR variables of the environment. The specified editor has to edit the file in place; editors which to unlink and recreate the file cannot be used. After exiting from the editor, the modified crontab will be installed automatically.
Below are few examples of using crontab with options.
The following command shows the configuration of crontab for the current user.
It might be something like this:
35 6 * * * cd /var/vhosts/site && /usr/local/bin/php runscripts.php
The next example shows how to edit crontab configuration for specified user - let's say robert:
crontab -e -u robert
After running this command, the crontab configuration of the specified user will be opened for editing. And after editing and closing the crontab will be stored here: /var/cron/tabs/robert
Removing crontab configuration can be done by the following command (in this case for the current user):
The crontab configuration contains a sequence of command lines and schedules their calls. Empty lines and line that begin with "#" are ignored. The rest of the lines are the configuration for cron.
Record in crontab-file generally consists of seven fields:
minutes hours day_of_month month day_of_week user_name command
Day of month
Day of week
0-7 (0-Sun, 1-Mon, 2-Tues, 3-Wed, 4-Thurs, 5-Fri, 6-Sat, 7-Sun)
The field have specific value or set by time interval:
- asterisk - any number;
- intergers separated by a comma - specifying a discrete set of values;
- two intergers separated by a hyphen, that correspond to the range of values.
An example of a finished configuration for cron:
# run task at 18:07 on May 13 if it’s Friday 7 18 13 5 5 /home/user/bin/somescript # run task every hour 0 */1 * * * /home/user/bin/somescript # run task every 7 hours 0 minutes 0 */7 * * * /home/user/bin/somescript # run task every Sunday at 10:30 30 10 * * 0 /home/user/bin/somescript
But before doing this the editor for crontab must be specified. For these purposes any editor can use.
Replace the current editor for editing crontab
If you run the command for editing crontab:
An editor will appear with the configuration of the crontab. But this editor can be tricky and difficult to use. To change the editor follow a few steps before you start editing the crontab.
Here are the steps:
- Find the installed editor in the system intended these purposes;
- Find out which shell is used in the system;
- Change the variable of environment by running the command shown in the example below.
If Midnight Commander is installed in the system use its own editor. It's a user-friendly and functional editor.
Before replacing the previous editor, find out the name of the shell:
Run only the first command if "csh" shell is used, or run only the second command if "bash" is used:
setenv VISUAL mcedit export VISUAL=mcedit