[Linux] Scheduled Task Scheduling

Scheduled task scheduling

crond task scheduling

 crontab for scheduled task scheduling

Description of the 5 placeholders

Description of special symbols

crond application example

at timed task scheduling

at execution task case 

 


Scheduled task scheduling

crond task scheduling

crond is a background program

 crontab for scheduled task scheduling

Overview:

Task scheduling: refers to a specific command or program that the system executes at a certain time

Classification of task scheduling:

1. System work: Some important work must be performed over and over again. such as virus scanning

2. Individual user work: Individual users may wish to perform certain programs, such as backups of mysql databases

Basic syntax: crontab [options]

Common options

-e Edit crontab timed tasks
-l Query crontab tasks
-r Delete all crontab tasks of the current user

Quick start

Set the task schedule file: /etc/crontab

Set personal task schedule, execute crontab -e

Then enter the task into the schedule file

Such as: first enter crontab -e, then enter the following

*/1* * * * ls -l /etc/ > /tmp/to.txt (overwrite the files under etc into to.txt every minute)

Description of the 5 placeholders

project meaning Scope
First" * " minute of the hour 0-59
the second" * " hours of the day 0-23
The third" * " day of the month 0-31
the fourth" * " month of the year 1-12
the fifth" * " day of the week 0-7 (both 0 and 7 represent Sunday)  

Description of special symbols

special symbols meaning
* represent any time. For example, the first "*" means to execute every minute of an hour.
Represents discontinuous time. For example, "0 8,12,16 * * * command" means that the command will be executed once every day at 8:00, 12:00 and 16:00
- Represents a continuous time range. For example "0 5 * * 1-6 command", which means to execute the command at 5:00 am from Monday to Saturday 
*/n Represents how often each execution is performed, such as "*/10 * * * * command", which means that the command is executed every 10 minutes

Cases of tasks performed at a specific time

crond application example

Example 1: Automatically append the current date information to the /home/mydate file every minute

crondtab  -e

*/1 * * * * date >> home/mydate
[root@kongchao02 home]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@kongchao02 home]# cat /home/mydate 
2022年 03月 08日 星期二 21:10:02 CST
[root@kongchao02 home]# cat /home/mydate 
2022年 03月 08日 星期二 21:10:02 CST
2022年 03月 08日 星期二 21:11:01 CST
[root@kongchao02 home]# 

 Example 2: Append time and calendar to mycal every minute

step:

The first step: vim my.sh (write date >> /home/mycal cal >> /home/mycal).sh is the shell script suffix

Step 2: chmod u+x my.sh (add execution rights to the owner)

Step 3: crontab -e enter and write (*/1 **** /home/my.sh)

Step 4: cat to view mycal

[root@kongchao02 home]# vim my.sh
[root@kongchao02 home]# cat my.sh 
date >> /home/mycal
cal >> /home/mycal

[root@kongchao02 home]# ll my.sh 
-rw-r--r--. 1 root root 40 3月   8 21:36 my.sh
[root@kongchao02 home]# chmod u+x my.sh 
[root@kongchao02 home]# ll my.sh 
-rwxr--r--. 1 root root 40 3月   8 21:36 my.sh
[root@kongchao02 home]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@kongchao02 home]# ll
总用量 32
drwxr-xr-x.  5 kongchao  fruit     4096 3月   7 09:47 apple
drwxr-xr-x.  2 root      root      4096 3月   5 16:25 home
drwx------.  3 kc        kc        4096 3月   5 09:35 kc
drwx------. 15 kongchao  kongchao  4096 3月   1 20:37 kongchao
drwx------.  5 kongchao1 kongchao1 4096 2月  28 22:08 kongchao1
drwx------.  3 kongchao2 kongchao2 4096 2月  28 22:11 kongchao2
-rw-r--r--.  1 root      root       194 3月   8 21:39 mycal
-rwxr--r--.  1 root      root        40 3月   8 21:38 my.sh
[root@kongchao02 home]# cat mycal
2022年 03月 08日 星期二 21:39:01 CST
      三月 2022     
日 一 二 三 四 五 六
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

2022年 03月 08日 星期二 21:40:01 CST
      三月 2022     
日 一 二 三 四 五 六
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

 service crond restart (restart task scheduling)

crondtab -r (remove task scheduler)

crondtab -l  (view current task schedules)

 Example 3: Back up the mysql database kc_db01 to a file at 3:00 am every day, prompt:

The command is: mysqldump -u root -p password database > /home/db.bak

Step 1: crontab -e

Step 2:02 * * * mysqldump -u root -p password database > /home/db.bak

at timed task scheduling

basic introduction

1. Unlike crond, at is a one-time execution of scheduled tasks, while crond is a cycle of execution

2. The atd daemon will run in background mode. Check the task queue to run.

3. By default, the atd daemon checks the task queue every 60 seconds. If there is a task, it will check the running time of the task, and run the task if the time matches the current time.

4. The at command is a one-time scheduled task. After a task is executed, the task will not be executed.

When using the at command, be sure to start the atd process, you can use the relevant commands to view

Command: ps -ef |grep atd 

Execution diagram: 

 at command format : at [options] [time]

Enter Ctrl+D twice to end the input of the at command

Options for the at command

Options meaning
-m Send the user an email when the specified task has been completed, even if there is no standard output
-I (capital i) alias for atq
-d alias for atrm
-v Shows when the task will be executed
-c print the contents of the task to standard output
-V Display version information
-q <queue> use the specified queue
-f <file> Read task from specified file instead of standard input
-t <time parameter> Submit the task to be run as a time parameter

at time definition

1. Accept the time designation in hh:mm (hours:minutes) of the day. If the time has passed, it will be executed on the next day. Example: 04:00

2. Use vague words such as midnight (late night), noon (noon), teatime (tea time, usually 4 pm) to specify the time.

3. The 12-hour time system is adopted, that is, adding AM (morning) or PM (afternoon) to the time to indicate whether it is morning or afternoon. Example: 12pm

4. Specify the specific date when the command is executed. The specified format is month day (month day) or mm/dd/yy (month/day/year) or dd.mm.yy (day.month.year). The specified date must be followed by after the specified time. For example: 04:00 2021-03-1

5. Use relative timing. The specified format is: now+count time-units, now is the current time, time-units is the time unit, here can be minutes (minutes), hours (hours), days (days), weeks (weeks). count is the amount of time, days, hours. Example: now + 5 minutes

6. Directly use today (today) and tomorrow (tomorrow) to specify the time to complete the command.

at execution task case 

Example 1 : Execute /bin/ls /home at 5:00 pm 2 days later, and use the atq command to check whether there are any work tasks executed in the system

[root@kongchao03 ~]# at 5pm + 2 days
at> /bin/ls /home<EOT>       
job 1 at Fri Mar 11 17:00:00 2022
[root@kongchao03 ~]# atq
1	Fri Mar 11 17:00:00 2022 a root
[root@kongchao03 ~]# 

 Example 2: After 20:00 tomorrow, output the time to the specified folder, such as /root/date100.log

[root@kongchao03 ~]# at 8pm tomorrow
at> date > /home/date100.log<EOT>
job 2 at Thu Mar 10 20:00:00 2022
[root@kongchao03 ~]# atq
2	Thu Mar 10 20:00:00 2022 a root
1	Fri Mar 11 17:00:00 2022 a root
[root@kongchao03 ~]# 

 Example 3 : Write date to date200.log after two minutes

[root@kongchao03 ~]# at now + 2 minutes
at> date > /home/date200.log<EOT>
job 3 at Wed Mar  9 21:12:00 2022
[root@kongchao03 ~]# atq
3	Wed Mar  9 21:12:00 2022 a root
2	Thu Mar 10 20:00:00 2022 a root
1	Fri Mar 11 17:00:00 2022 a root
[root@kongchao03 ~]# cat /home/date200.log 
2022年 03月 09日 星期三 21:12:00 CST
[root@kongchao03 ~]# 

 Example 4 : Delete already executed task, atrm number

[root@kongchao03 ~]# atq
2	Thu Mar 10 20:00:00 2022 a root
1	Fri Mar 11 17:00:00 2022 a root
[root@kongchao03 ~]# atrm 1
[root@kongchao03 ~]# atq
2	Thu Mar 10 20:00:00 2022 a root
[root@kongchao03 ~]# 

 

Guess you like

Origin blog.csdn.net/weixin_60719453/article/details/123595298