The at command has been introduced in previous articles, which is used to set a scheduled task that is executed only once. Today, let’s take a look at the scheduled tasks that are executed periodically. In work, periodic scheduled tasks are often used, such as scheduled backup of system files, scheduled backup of database files, processing orders that have not been paid for a long time, and so on.
Users set periodic crontab
General users who want to set periodic scheduled tasks need to use the command crontab. Like at, crontab also has black and white lists, and the configuration files for them are – /etc/cron.deny, /etc/cron.allow.
When the user sets a scheduled task, the task will be recorded in the /var/spool/cron directory as a text file.
# ll /var/spool/cron/ total 4 -rw------- 1 root root 98 Jul 28 03:58 root
About crontab, we only need to remember two commands
-
crontab -e set scheduled tasks
-
crontab -l view scheduled tasks
The format for setting a scheduled task is as follows:
Basic format: time-sharing date month week command Value range: [0-59] [0-23] [1-31] [1-12] [0-7]
In addition, we also need to remember a few special characters Meaning:
-
* means any time can be
-
, means separation The meaning of the time period
-
– means a continuous period of time
-
/n means every n unit interval
Let’s take a few examples to see the usage of the above special characters:
# Every 5 minutes, execute Script to check memory */n * * * * /root/script/check_memory.sh # At 5:20 am on the 1st of every month, restart the server 20 5 1 * * /usr/sbin/reboot
system configuration files /etc/crontab, /etc/cron.d/ *
Regarding periodic scheduled tasks, there are mainly three corresponding configuration files, which are
-
/var/spool/cron/, set for users
-
/etc/crontab, scheduled tasks for the system
-
/etc/cron.d/ Timing tasks for a certain software and system, for example, for a website, there may be multiple timing tasks, so it is recommended to be in /etc/cron.d Under the / directory, create a new configuration file
crontab -e is designed for the user level. For periodic tasks at the system level, it is recommended to write in the /etc/crontal file In, or under the /etc/cron.d/ directory.
Look at the contents of the /etc/crontab file below
# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=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 # * * * * * root /usr/bin/date >> /root/cron.txt
As you can see, it is slightly different from crontab -e, and a user name needs to be specified here.
Next, let’s take a look at the files in the /etc/cron.d/ directory
# ll /etc/cron .d total 8 -rw-r--r--. 1 root root 128 Aug 3 2017 0hourly -rw------- 1 root root 235 Aug 3 2017 sysstat
If there is a website on the server, for this website, we need to back up the database and website files regularly, and also need to process regularly Some data, then we can create a related file in the /etc/cron.d/ directory. Let’s take a look at the content of the 0hourly file:
# Run the hourly jobs SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root 01 * * * * root run-parts /etc/cron.hourly
The meaning of this script is to execute the script file in the /etc/cron.hourly directory every hour.
It is estimated that sharp-eyed friends have discovered that in addition to the cron.hourly directory, there are also cron.daily, cron.monthly, and cron.weekly directories. But these three directories are still a little different, these three directories are executed by anacron. This anacron means to wake up the unexecuted scheduled task and execute it. For example, when you use crontab -e to set up a scheduled task, due to shutdown or other reasons, the single task is not executed after the time has passed, then you can only miss it, but if you place the script in /etc/cron Under the .daily/ directory, as long as it is not executed, it will still be executed even if the time has elapsed.
The above is the detailed content of the scheduled task under linux – the periodically executed scheduled task. For more information, please pay attention to other related articles on 1024programmer.com!