Command Cron in Linux: Schedule Automatic Tasks with Crontab
The cron command on Linux is a service that provides task scheduling for users and the system.
It allows a command, program, or script to be scheduled for a specific day, month, year, and time. It is widely used for tasks that need to run every hour, day, or any other period, on a recurring basis.
Usually, cron is used to execute log archival and system integrity checking, as well as other periodic maintenance tasks.
The system tasks that will be executed are defined in the file /etc/crontab.
Each user can also have their own tasks, configured in individual files in the directory /var/spool/cron/ using 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, you can use the crontab command or edit the file /etc/crontab with any text editor and define when the task will be executed.
The format used by the cron file is:
MINUTOS HORAS DIA MES SEMANA USUARIO COMANDO
Crontab Format |
It is not necessary to restart the cron service because it checks its files every minute.
The format of a crontab file follows this order:
The values for each field can be:
Minute: Value between 0 and 59;
Hour: Value between 0 and 23;
Day of Month: Value between 1 and 31;
Month: Value between 1 and 12 (identifying January through December);
Day of Week: Value between 0 and 7, where zero or seven are used to identify Sunday.
User Login: The user who will be used to execute the command;
Command: Program to 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 slash "/". Example "*/5" in the minutes field indicates that the command should run every 5 minutes.
It is possible to specify intervals for command execution using the hyphen "-", indicating the execution periods including the start and end numbers.
A list of times and dates can be made using the comma " ," to separate values.
Here are some examples of cron:
Runs the sync command every day at 10:00:
0 10 * * * root sync
Runs the updatedb command every Monday at 06:00:
0 6 * * 1 root updatedb
Runs the runq command every twenty minutes:
0,20,40 * * * * root runq
Sends an email at 0:15 every Christmas to carla:
15 0 25 12 * root mail carla < natal.txt
Runs the poff command at 5:30 on Monday through 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 easier 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 it does not have the user field (UID), since the file name already has this identification.
# ls –l /var/spool/cron/tabs-rw——— 1 root root 264 Jun 20 14:36 root-rw——— 1 root users 199 Jun 20 15:58 uira
The most frequent crontab options are:
- -e: Edits the cron file using the vi editor.
- -l: Lists the contents of the current user's cron file
- -r: Removes the cron file.
- -u user: Performs one of the operations as if it were a specific user. Must be used together with the other options and only root can change other users' cron.
See the examples:
In this example the command "/usr/bin/mrtg /etc/mrtg.cfg" is executed every 5 minutes and "/bin/sendip.sh" every day at 01:00am:
$ crontab –l*/5 * * * * /usr/bin/mrtg /etc/mrtg.cfg0 1 * * * /bin/sendip.sh
To schedule a task in cron, use "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 locations. They are:
/etc/cron.allow
If the file cron.allow exists, a specific user needs to have their login listed in its contents to have explicit permissions to use cron.
/etc/cron.deny
If the file cron.deny exists, the logins listed in its contents are prohibited from using cron. All other users will be able to use cron.
If neither file exists, cron usage will be allowed for all users. If the cron.deny file is empty, all users will have permission to use cron.
Special Cron Directories
/etc/cron.d
Cron also has a special directory at /etc/cron.d. This directory may contain the following files:
- daily: specifies which commands are executed daily;
- hourly: specifies which commands are executed hourly;
- weekly: specifies which commands are executed weekly;
- monthly: specifies which commands are executed monthly.
The files contained in this directory must follow the format of /etc/crontab:
# cat /etc/cron.d/sysstat */10 * * * * root /usr/lib64/sa/sa1 1 153 23 * * * root /usr/lib64/sa/sa2 -A
All scripts or commands executed are under the root user permission.
Files in /etc/cron.d usually have their names preceded by an integer greater than zero to order their execution
/etc/cron.{daily, hourly, weekly, monthly}
Other possibilities that cron examines are the directories /etc/cron.daily, /etc/cron.hourly, /etc/cron.weekly and /etc/cron.monthly.
These directories can contain scripts that will be executed at the time intervals that each file name specifies.
# ls -l /etc/cron.daily/-rwx------. 1 root root 219 Apr 10 2018 logrotate-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, files must contain scripts with commands that will be executed, not a schedule of what will be executed like in crontab.
See the logrotate script example:
$ cat /etc/cron.daily/logrotate #!/bin/sh/usr/sbin/logrotate /etc/logrotate.confexit 0
Another important detail: scripts in /etc/cron.{daily, hourly, weekly, monthly} need to have the executable permission "X" enabled.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.
Crontab Format