Linux task management tool-Crontab
Crontab is a tool used to execute routine tasks cyclically on the Linux platform. in Linux, cron (crond) is a system service used to control tasks, in Linux, many scheduled tasks need to be started. Therefore, this system service is started by default. The Command provided by Linux to schedule tasks is Crontab.
Crontab is used in Linux to periodically execute tasks or wait for some tasks to be processed.
Crontab configuration file
Crontab periodic tasks are divided into two types: system tasks/user tasks.
- A system task is a task that the system periodically needs to execute, for example, a cache cleaning task is regularly executed, and logs are recorded. The configuration file of the system task is in/etc/crontab.
Here is the content in the configuration file of crontab.
SHELL=/bin/shPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root# For details see man 4 crontabs# Example of 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
The first four rows are used to configure cron environment variables. The configuration of environment variables is not discussed here. The following content is a simple explanation of crontab usage.
- You can use the crontab tool for your periodic tasks. All your crontab scheduled tasks are saved in/var/spool/cron. The file name is the user name.
Meaning of the Crontab file
The preceding crontab file contains a part of the explanation. Here I translate it into Chinese
SHELL =/bin/sh PATH =/sbin:/bin:/usr/sbin:/usr/binMAILTO = root # For details, refer to the man manual # example :#. ---------------- minute (0-59) # |. ------------- hour (0-23) # |. ---------- date (monthly) (1-31) # |. ------- month (1-12) can also use monthly English, such as jan, feb, mar, apr... # |. ---- date (weekly) (0-6) (Sunday is 0 or 7) sun, mon, tue, wed, thu, fri, sat # | # *** the user who wants to execute the command (this parameter can be blank) The command you want to execute
Crontab contains seven fields. The first five fields are about setting the execution cycle. The sixth field is used to specify the user who wants to execute the command. The seventh field is the command to be executed.
The following special characters can be used in the first five fields
Asterisk (*): represents all possible values. For example, if the month field is an asterisk, this command is executed every month after the conditions of other fields are met.
Comma (,): values separated by commas (,) can be used to specify a list range, for example"
Middle bars (-): You can use the middle bars between Integers to represent an integer range. For example, "2-6" indicates "2, 3, 4, 5, 6"
Forward slash (/): You can use a forward slash to specify the interval of time. For example, "0-23/2" indicates that execution is performed every two hours. At the same time, the forward slash can be used with the star number, for example, */10. If it is used in the minute field, it indicates that the execution is performed every ten minutes.
Related permission files
- /Etc/cron. deny the users listed in this file cannot use the crontab command
- /Etc/cron. allow users listed in this file can use the crontab command
- /Var/spool/cron/directory where all user crontab files are stored. The file name is the user name.
Crontab Service
- Install the Crontab Service
CentOS 7.x
yum install crontabs
Fedora 23 +
dnf install crontabs
In most Linux releases, the built-in Crontab still writes the installation method.
Enable Service
systemctl start crond
Stop Service
systemctl stop crond
View service status
systemctl status crond
Add boot start
systemctl enable crond
Cancel startup
systemctl disable crond
Command Format details
Usage: crontab [options] filecrontab [options] crontab-n [hostname] Options:-u <user> define user // sets a user's crontab service. For example, "-u user1" indicates setting the crontab service of user1. this parameter is generally run by the root user-e edit user's crontab // edit the contents of the crontab file. If no user is specified, the crontab file-l list user's crontab is edited. // the contents of the crontab file are displayed. If no user is specified, the contents of the current user's crontab file are displayed.-r delete user's crontab // delete the crontab file of a user. If no user is specified, the crontab file of the current user is deleted by default, this action is generally performed by the root user-I prompt before deleting // a confirmation prompt is displayed when the user's crontab file is deleted-n
Usage
- Enter crontab-e and press enter to edit the current user's crontab file. After the editing is complete, the file takes effect immediately.
By default, the vi EDITOR is used. If you want to use the vim EDITOR or other editors, you can create an environment variable EDITOR. The variable value is the EDITOR command you want to use. For example, if you want to use nano as the EDITOR, you can run the "export EDITOR = nano" command to add an environment variable.
To make the environment variables take effect permanently, you can write the commands to be executed to "~ /. Bashrc ", which will be automatically configured at the next startup.
- To delete a crontab file, you can use crontab-r to delete the crontab file of the current user.
- To quickly view the crontab file, run the crontab-l command.
Back up the crontab file To prevent accidental deletion of files, we can back up the relevant configuration files after setting up the crontab task. The idea is to copy the current document and add ". backup is a backup file. Generally, you only need to execute the following command (take the root user as an example)
cp /var/spool/cron/root /var/spool/cron/root.bachup
If you do not have the permission, you can copy your configuration file to your HOME directory first.
Some simple examples Restart the Apache service at on the first day of each month.
30 2 1 * * systemctl restart httpd
Restart mariadb at every Monday of the week.
0 1 * * 1 systemctl restart mariadb
More examples can be easily found by searching.
Notes Generally, the crontab tasks we create are saved and executed immediately, but sometimes they cannot be executed. The commands can be used separately, at this time, you need to check whether the environment variables of the crontab file are normal. Cron is not a shell and does not know the environment variables during execution. Therefore, provide necessary environment variable files and paths in scripts or configuration files. Pay attention to the following points:
- When a path is involved in the configuration file, use the absolute path
- When python, java, or other environment variables are required for task execution, you must use the source command to introduce the environment variables.
- In some cases, you can manually execute the script, but it cannot be executed in Crontab. It is very likely that the environment variable causes a fault. You can directly introduce the environment variable in crontab.
Other items
- It is strange that after I read a lot of information, I found that after the crontab task is executed, I will send an email to the current system user, however, my crontab did not receive any emails during the two-day task.
- The crontab file may not be executed immediately after it is written. The execution delay is about 2 minutes. If the crond service is restarted, It will be executed immediately.
- It is said that % has a special meaning in crontab, indicating the line feed. However, this phenomenon was not found in the author's verification.
- If you only run crontab-r, it is possible to delete the crontab of all users in the crontab directory.
Use crontab in Linux to create scheduled tasks
Routine scheduling of crontab in Linux
Linux crontab does not run troubleshooting
Ubuntu uses crontab for scheduled tasks
Linux scheduled task (at batch crontab anacron)
Linux task scheduler (at, crontab)
This article permanently updates the link address: