Categories: How-To

Cron command on Linux (task scheduling) [Basic Guide]

The cron command in Linux is a service that provides the task scheduling service for users and the system.

It allows a command, program, or script to be scheduled for a particular day, month, year, and time. It is widely used in tasks that need to be performed every hour, day, or any other period, on a recurring basis.

Cron is generally used to perform log archiving and system integrity checks and other periodic maintenance.

The

system tasks that will be executed are defined in the /etc/crontab file.

Each user can also have their tasks configured in individual files in the /var/spool/cron/ directory with the user’s account login.

Cron has a utility called crontab that makes it easier to view and edit cron files.

To schedule a new task, we can use the crontab command or edit the /etc/crontab file with any text editor and define when the task will be executed.

The format that the cron file uses is:

MINUTES HOURS DAY MONTH WEEK USER COMMAND
Crontab format

There is no need to restart the cron service because it checks your files every minute.

The format of a crontab file follows the following order:

The values for each field can be:

Minute: Value between 0 and 59;
Hour: Value between 0 and 23;
Day of the Month: Value between 1 and 31;
Month: Value between 1 and 12 (identifying the months of January to December);
Day of the Week : Value between 0 and 7, with zero or seven being used to identify Sunday.
User Login: The user that will be used to execute the command;
Command: Program that will be executed, plus its normal parameters.

Each field must be separated by one or more spaces or tabs.

The asterisk symbol “*” can be used to indicate that any date or time is valid. It is also possible to specify time intervals using the “/” bar. Example “*/5″ in the minutes field indicates that the command should run every 5 minutes.

It is possible to specify intervals for the command to be executed using the hyphen “”, indicating the execution periods including the starting and ending number.

A list of times and dates can be made using the comma “,” to separate the values.

Here are some examples of cron:

Run the sync command every day at 10:00:

0 10 * * * root sync

Run the updatedb command every Monday at 06:00:

0 6 * * 1 root updatedb

Run the runq command every day every twenty minutes:

0,20,40 * * * * root runq

Send an e-mail at 0:15 every Christmas to Carla:

15 0 25 12 * root mail Carla < natal.txt

Run the poff command at 5:30 from Monday to Saturday:

30 5 * * 1-6 root poff

Runs a PHP script every 5 minutes:

*/5 * * * * root /usr/bin/php -c /etc/php.ini /home/script.php

Crontab file

The crontab command makes it easy to edit cron configuration files. It is useful for editing user files located in /var/spool/cron/

Crontab saves the individual cron file with the user’s login name.

This file has the same format as /etc/crontab except that it does not have a user field (UID), since the file name already has this identification.

# ls —l /var/spool/cron/tabs -RW———1root264Jun 20 14:36 root -RW———1rootusers199Jun 20 15:58 hours

The most common crontab options are:

  • -e: Edit the cron file using the vi editor.
  • -l: Lists the content of the current user’s cron file
  • -r: Removes the cron file.
  • -u user: Perform one of the operations as if it were a specific user. It must be used in conjunction with the other options and only root can change the cron of other users.

See the examples:

In this example the command “/usr/bin/mrtg /etc/mrtg.cfg” is executed every 5 minutes and the “/bin/sendip.sh” every day at 01:00am:

$ crontab —l */5 * * * * * /usr/bin/mrtg /etc/mrtg.cfg 0 1 * * /bin/sendip.sh

To schedule a task in cron, use the “crontab —e” to edit the file with vi:

$ crontab -e

Include a blank line at the end of the file, otherwise the last command will not be executed.

Cron permissions

Cron has two files that enable or disable its use by users. Depending on the distribution, they may have different names and places. They are:

/etc/cron.allow

If the cron.allow file exists, a particular user must have their login listed in its content to have express permissions to use cron.

/etc/cron.deny

If the cron.deny file exists, the logins listed in its content are prohibited from using cron. Other users will be able to use cron.

If neither file exists, the use of cron will be allowed for all users. If the cron.deny file is empty, all users are allowed to use cron.

Cron Special Directories

/etc/cron.d

Cron also has a special directory at /etc/cron.d. This directory may contain the files:

  • daily: specifies which commands are executed daily;
  • hourly: specifies which commands are executed on an hourly basis;
  • weekly: specifies which commands are executed every week;
  • monthly: specifies which commands are executed every month.

The files contained in this directory must follow the /etc/crontab format:

# cat /etc/cron.d/sysstat */10 * * * * root /usr/lib64/sa/sa1 1 1 53 23 * * * root /usr/lib64/sa/sa2-A

All scripts or commands executed are under the permission of the root user.

The files in /etc/cron.d usually have their name preceded by an integer greater than zero to order their execution

/etc/cron. {daily, hourly, weekly, monthly}

Other possibilities that cron examines are the /etc/cron.daily, /etc/cron.hourly, /etc/cron.weekly, and /etc/cron.monthly directories.

These directories may contain scripts that will run at the time intervals specified by each file name.

# ls -l /etc/cron.daily/ -rwx------. 1 root root 219 Apr 10 2018 achieved -rwxr-xr-x. 1 root root 618 Mar 17 2014 man-db.cron -rwx------. 1 root root 208 Apr 10 2018 mlocate

In these directories, the files must contain scripts containing commands that will be executed, and not a schedule of what will be executed as in crontab.

See the example of the logrotate script:

$ cat /etc/cron.daily/logrotate 
#! /bin/sh

/usr/sbin/logrotate /etc/logrotate.conf
exit 0

Another important detail: the scripts in /etc/cron. {daily, hourly, weekly, monthly} must have the executable permission “X” enabled.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Share
Published by
Uirá Endy Ribeiro

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.