How to Use the crontab Command
The crontab command is commonly found in Unix and Linux operating systems and is used to set commands that are executed periodically. This command reads instructions from the standard input device and stores them in the “crontab” file for later reading and execution.
In Linux systems, task scheduling mainly falls into the following two categories:
1. Tasks executed by the system: tasks that the system needs to perform periodically, such as backing up system data and clearing the cache
2. Tasks executed by individuals: tasks that a user needs to do regularly, such as checking whether there is new mail on the mail server every 10 minutes; these tasks can be configured by each user individually
I. Introduction to the /etc/crontab, /etc/cron.deny, and
/etc/cron.allow Files
System-scheduled tasks are generally stored in the /etc/crontab file, which contains some scheduling programs used by the system during operation. We can use a command to view its contents:
[[email protected] ~]# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # For details see man 4 crontabs # Example job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed
These tasks are all automatically scheduled by the system after it starts running. At the same time, the /etc directory also contains the /etc/cron.deny
and /etc/cron.allow files.
/etc/cron.deny
indicates the users who are not allowed to use the crontab command.
/etc/cron.allow
indicates the users who are allowed to use crontab.
If both files exist at the same time, then /etc/cron.allow takes precedence.
If neither file exists, then only the root user can schedule jobs.
II. crontab command syntax and the meaning of each parameter
The syntax of the crontab command is:
crontab [-e [UserName]|-l [UserName]|-r [UserName]|-v [UserName]|File ]
Note: crontab
is used to allow users to execute programs at fixed times or fixed intervals; in other words, it is similar to a user’s schedule. -u
user means setting the schedule for the specified user,
provided that you have the required permissions (for example,
root) in order to specify another person’s schedule. If -u user
is not used, it means setting your own schedule.
Explanation of each parameter:
-e [UserName]: Launch a text editor to set the schedule; the default text editor is vi -r [UserName]: Delete the current schedule -l [UserName]: List the current schedule -v [UserName]: List the status of the user's cron jobs
3. The format of the crontab command
If we want to create our own scheduled task, we can use the
crontab -e
command, for example:
[[email protected] ~]# crontab -e
At this point, you will enter the vi editing interface to write the task you want to schedule. The format of the crontab scheduling command is as follows:
* * * * * command path // The first five fields can take integer values to specify when the job should start running. The sixth field is a string, namely the command field, which includes the command executed by crontab scheduling. The fields are separated by spaces and tabs.
Rules for scheduling commands:
| Field Name | Description | Value Range |
| Minute | Executes at which minute of every hour | 0-59 |
| Hour | Executes at which hour of every day | 0-23 |
| Date | Executes on which day of every month | 1-31 |
| Month | Executes in which month of every year | 1-12 |
| Weekday | Executes on which day of every week | 0-6 |
| Command Name | The command and parameters to be executed |
Some commonly used special symbols in the crontab command:
| Symbol | Description |
| * | Represents any time |
| , | Indicates separation |
| - |
Indicates a range, for example in the second field: 1-5 means from 1 to 5 o’clock |
| /n |
Indicates execution once every n units. For example, in the second field, */1, means executing the command once every 1 hour. It can also be written as 1-23/1. |
Below are some examples of crontab commands:
00 8,12,16 * * * /data/app/scripts/monitor/df.sh 30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh 10 8,12,16 * * * /data/app/scripts/monitor/check_ind_unusable.sh 10 8,12,16 * * * /data/app/scripts/monitor/check_maxfilesize.sh 10 8,12,16 * * * /data/app/scripts/monitor/check_objectsize.sh 43 21 * * * Run every day at 21:43 15 05 * * * Run every day at 05:15 0 17 * * * Run every day at 17:00 0 17 * * 1 Run every Monday at 17:00 0,10 17 * * 0,2,3 Run every Sunday, Tuesday, and Wednesday at 17:00 and 17:10 0-10 17 1 * * On the 1st of every month, run every 1 minute from 17:00 to 7:10 0 0 1,15 * 1 Run at 0:00 on the 1st and 15th of every month and on Mondays 42 4 1 * * Run at 4:42 on the 1st of every month 0 21 * * 1-6 Run Monday through Saturday at 21:00 0,10,20,30,40,50 * * * * Run every 10 minutes */10 * * * * Run every 10 minutes * 1 * * * Run every 1 minute from 1:0 to 1:59 0 1 * * * Run at 1:00 0 */1 * * * Run every hour on the hour 0 * * * * Run every hour on the hour 2 8-20/3 * * * Run at 8:02,11:02,14:02,17:02,20:02 30 5 1,15 * * Run on the 1st and 15th at 5:30
4. Create our own crontab command
① Example 1: If I want to write the system time to the date1.txt file in the /home directory every minute, then enter the following command
[[email protected] ~]# crontab -e In the crontab command editing interface, enter the crontab command: * * * * * date >> /home/date1.txt (The first five * * * * * indicate the time interval is every minute; the date command gets the current system time; the >> command appends the result to the end of the file; the > command overwrites the file with the result.)
After saving, if the message crontab: installing new crontab appears, it means the scheduled command has been set successfully We can go to the /home directory to see whether this text file has already been created, and check the information inside it:
[[email protected] home]# cat date1.txt Sat Apr 6 16:15:09 CST 2013 Sat Apr 6 16:16:02 CST 2013 Sat Apr 6 16:17:01 CST 2013
At this point, we can see that the crontab command we just wrote is already running normally. As long as no stop command is executed and the system is not shut down, this command will continue running.
② Example 2: If we need to complete two commands at the same time, that is, write the system time to the date1.txt file in the /home directory every minute, and also copy that date1.txt file to the / directory every minute to create a date2.txt file there, then what should we usually do? There are two methods:
a) The simplest and most direct method (not recommended): directly enter the crontab -e
command, and then append another command after it, for example:
[[email protected] ~]# crontab -e * * * * * date >> /home/date1.txt * * * * * cp /home/date1.txt /date2.txt
Finally, just exit and save. At this point, we can see that the file date2.txt already exists in the / directory. After opening it, you can see that its contents are the same as date1.txt:
[[email protected] /]# cat date2.txt Sat Apr 6 16:15:09 CST 2013 Sat Apr 6 16:16:02 CST 2013 Sat Apr 6 16:17:01 CST 2013 Sat Apr 6 16:18:01 CST 2013 Sat Apr 6 16:19:01 CST 2013 Sat Apr 6 16:20:01 CST 2013 Sat Apr 6 16:21:01 CST 2013 Sat Apr 6 16:22:01 CST 2013 Sat Apr 6 16:23:02 CST 2013
Although this method is relatively simple, it is not recommended, because if there are many commands, and if I need different users to execute different commands, this method is inconvenient to maintain. So here I will introduce another method.
b)
First, create an executable sh file, then write the tasks we want to execute into the sh file, and finally use crontab to execute our sh file (recommended)
First, we create a file named task.sh in the / directory. Its contents are the two commands we want to execute:
[[email protected] /]# vi task.sh Commands inside task.sh date >> /home/date1.txt cp /home/date1.txt /date3.txt
At this point, our task.sh is not yet an executable file. By using the ls
-l command, we can see:
-rw-r--r--. 1 root root 54 Apr 6 16:27 task.sh //task.sh is not executable, we need to modify its permissions
So we need to use the chmod command to change the permissions of task.sh:
[[email protected] /]# chmod 744 task.sh
Checking again now, you will find that task.sh is already an executable file:
-rwxr--r--. 1 root root 54 Apr 6 16:27 task.sh
Then enter the crontab -e
command, delete the previous two commands, or add a # in front to comment them out:
#* * * * * date >> /home/date1.txt #* * * * * cp /home/date1.txt /date2.txt * * * * * /task.sh
At this point, we find that the file date3.txt already exists in the root directory. Opening it, we can see that its contents are:
[[email protected] /]# cat date3.txt Sat Apr 6 16:15:09 CST 2013 Sat Apr 6 16:16:02 CST 2013 Sat Apr 6 16:17:01 CST 2013 Sat Apr 6 16:18:01 CST 2013 Sat Apr 6 16:19:01 CST 2013 Sat Apr 6 16:20:01 CST 2013 Sat Apr 6 16:21:01 CST 2013 Sat Apr 6 16:22:01 CST 2013 Sat Apr 6 16:23:02 CST 2013 Sat Apr 6 16:24:01 CST 2013 Sat Apr 6 16:25:01 CST 2013 Sat Apr 6 16:26:01 CST 2013 Sat Apr 6 16:27:01 CST 2013 Sat Apr 6 16:28:01 CST 2013 Sat Apr 6 16:29:01 CST 2013 Sat Apr 6 16:31:02 CST 2013 Sat Apr 6 16:32:01 CST 2013
5. Some other crontab commands
If we need to list the task schedules we created ourselves, we can use the crontab -l
command to view them
[[email protected] /]# crontab -l #* * * * * date >> /home/date1.txt #* * * * * cp /home/date1.txt /date2.txt * * * * * /task.sh
If we need to terminate the task schedule we just created, simply use the
crontab -r command
[[email protected] /]# crontab -r
At this point we can see that all tasks in crontab have been removed
6. Starting the cron service
We can use the
chkconfig –list | grep cron
command to check the startup status of the cron service:
[[email protected] home]# chkconfig --list | grep cron crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
We can see that if the system startup level is 1-4, the cron service will start automatically at boot. We can use the following command to set the startup options for this service:
/sbin/service crond start /sbin/service crond stop /sbin/service crond restart /sbin/service crond reload The above lines 1-4 are used to start, stop, restart the service, and reload the configuration, respectively. To set cron to start automatically at boot, just add /sbin/service crond start to the /etc/rc.d/rc.local script.
This post mainly records the common usage of the crontab command in Linux systems. I will continue to record my learning notes in future studies.
