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 starts the cron service/sbin/service crond start at random.
Stop service/sbin/service crond stop
Restart the service/sbin/service crond restart
Restart/sbin/service crond reload the start and stop of the service. The following describes how to add scheduled tasks.
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-59 minutes) 2nd-23 hours)
3rd bits indicate Day 1-31) 4th bits indicate Month 1-12)
The 5th position indicates the day of the week 0-6) 0 indicates the day of the week
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. The following is an example of 8 bits:
Write a paragraph to a file at every day
0 1 *** echo "hello world">/tmp/test.txt
Execute a program or command every 5 minutes.
*/5 * how do you know whether 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 added, a username file is generated in the/var/spool/cron directory, the file contains your scheduled tasks, the cron service reads files in/var/spool/cron once per minute. You can also add a scheduled task and edit it by yourself./etc/crontab cron 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.
02 4 *** root run-parts/etc/cron. daily run the script in/etc/cron. daily once a day.
22 4 ** 0 root run-parts/etc/cron. weekly run the script in/etc/cron. weekly once a week.
42 4 1 ** root run-parts/etc/cron. monthly execute the script in/etc/cron. monthly every month
Note that the preceding run-parts parameter should be followed by a script name to be executed if the run-parts parameter is removed. If the run-parts parameter is added, it must be followed by the directory name. You can also restrict the use of cron.
If the cron. allow cron. 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.
- Linux mounting commands
- Detailed command 1 for running Linux background)
- Brief Introduction to Linux kernel Security Intrusion Detection System
- 10 new Linux knowledge points
- Comprehensive Analysis of Linux clock