Linux uses crontab command to create scheduled tasks

Task scheduling under Linux includes system task scheduling and user task scheduling. The configuration file for system task scheduling is /etc/crontab. The crontab files for user task scheduling are stored in the /var/spool/cron directory.

We can use the crontab command to create our timed tasks (that is, user task scheduling). For example, we append the current system time to the /home/likeke/demo.txt file every 1 minute :

1. Using the crontab -e command, the following file content appears (detailed usage instructions):

2. Next, enter the following commands under this prompt (five asterisks represent five time parameters, and these time parameters will be explained in detail later): 

* * * * * date -d now >> /home/likeke/demo.txt

  3. Press ctrl+x to exit, it will prompt whether to save, enter Y to save, and then prompt to save the file name, change it yourself or press Enter to use the default name

  4. Restart the cron service:

/etc/init.d/cron restart

  5. Complete, open /home/likeke/demo.txt, the effect is as follows:

  
Time parameter description: 

m h dom mon dow

 

m: Indicates minutes, can be any integer from 0 to 59.

h: Indicates the hour and can be any integer from 0 to 23.

dom: Represents a date, which can be any integer from 1 to 31.

mon: Indicates the month, which can be any integer from 1 to 12.

dow: Indicates the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.

 Time parameter usage example:

1: Execute command every 1 minute
Order:
* * * * * command
 
2: Execute once every hour at the 20th minute
Order:
20 * * * * command
 
3: Execute once a day at the 20th minute from 6 am to 8 am
Order:
20 6-8 * * * command
 
4: Execute once a day at the 20th and 40th minutes from 6:00 to 8:00 in the morning
Order:
20,40 6-8 * * * command
 
5: Execute once every Monday at the 20th minute from 6:00 am to 8:00 am
Order:
20 6-8 * * 1 command

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326487982&siteId=291194637
Recommended