Home / Articles / BSD / FreeBSD / How to use crontab command in FreeBSD

How to use crontab command in FreeBSD

How to use crontab command in FreeBSD
 
How to use crontab command in FreeBSD
  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Rating: 5/5 (2 votes cast)

Thank you for rating!

You have already rated this page, you can only rate it once!

Your rating has been changed, thanks for rating!

Log in or create a user account to rate this page.


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 options

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.

crontab -l

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):

crontab -r

Crontab configuration

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

Parameter

Permissible range

Minutes

0-59

Hours

0-23

Day of month

1-31

Month

1-12

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;
  • integer;
  • 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:

crontab -e

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:

  1. Find the installed editor in the system intended these purposes;
  2. Find out which shell is used in the system;
  3. 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:

echo $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

 

Read also

Install MySQL server on FreeBSD

Install MySQL server on FreeBSD

Install Apache in FreeBSD

Install Apache in FreeBSD

Log in or create a user account to post a comment.

easyos.net
117217982212577402640

Quick navigation

General navigation