Linux scheduled task cron is a scheduled mode. for example, we can back up the system when the system is used, but the administrator cannot come to the company in the middle of the night.
Linux scheduled task cron is a scheduled mode. for example, if we can back up the system when the system is used, but the administrator cannot come to work at the company in the middle of the night, then we can use the Linux scheduled task cron, here we will introduce how to use it.
Currently, after installing popular Linux systems, cron is installed and started at random.
Start cron service/sbin/service crond start
Stop service/sbin/service crond stop
Restart the service/sbin/service crond restart
Reload/sbin/service crond reload
After the service is stopped, the following describes how to add a scheduled task.
Use the crontab command
Crontab-u sets a user's cron service
Crontab-l list the details of the current user's cron service
Crontab-r deletes the cron service of the current user
Crontab-e: Edit the cron service of the current user
For example, the current login user is root
Run crontab-e to create a scheduler task for the root user.
Run crontab-e to go to vi to edit the scheduler task content
1st-bit minutes (0-59) 2nd-bit hours (0-23)
3rd bits represent the day (1-31) 4th bits represent the month (1-12)
The 5th position indicates the week (0-6). 0 indicates Sunday.
Next is the task you want to execute
Separate each digit with spaces
In addition to numbers, the preceding time also contains several special symbols.
"*" Indicates all values. if the value is the first, "*" indicates every minute.
"/" Indicates that if the first digit is used */5, the value is 5 minutes.
"-" Indicates the value range "," to separate discrete values. for example, 2nd bits are 1-6, and 8 bits represent 1 to 6, and there are 8 points.
The specific instance is as follows:
Write a paragraph to a file at every day
0 1 *** echo "helloworld">/tmp/test.txt
Execute a program or command every 5 minutes.
*/5 *** command
How do you know if your scheduled task is running? 1. you can check whether the command you want to execute is running at the specified time, 2. some commands run poorly. you can check cron logs/var/log/cron.
This file records the execution of scheduled tasks.
After the preceding method is used to add a scheduler task, a username file is generated in the/var/spool/cron directory, which contains your scheduler task, the cron service reads files in/var/spool/cron once per minute.
You can also add a scheduled task to edit/etc/crontab by yourself (the cron service also reads the/etc/crontab file once per minute)
Adding a scheduled task at the end of the file is in the same format as the preceding method.
There are other things in/etc/crontab.
MAILTO = root // if an error occurs or data is output, the data is sent to this account as an email.
01 *** root run-parts/etc/cron. hourly execute the script in/etc/cron. hourly every hour.
0 24 *** run the script in/etc/cron. daily once a day
2 24 ** 0 root run-parts/etc/cron. weekly run the script in/etc/cron. weekly once a week.
4 24 1 ** root run-parts/etc/cron. monthly execute the script in/etc/cron. monthly every month
Note that if the preceding run-parts parameter is removed, a script name will be followed by the run-parts parameter. if the run-parts parameter is added, the directory name will be followed.
You can also restrict the use of cron.
If the cron. allowcron. deny file exists in the/etc/directory
Only users listed in the cron. allow file can use the cron service and ignore the cron. deny file.
If the cron. allow file does not exist, users listed in the cron. deny file will be prohibited from using the cron service.
In this way, we have completed the Linux scheduled task cron learning.