Linux scheduled tasks

Posted by hmogan on Thu, 11 Nov 2021 04:27:12 +0100

Linux scheduled tasks

Scheduled task type

There are two types of scheduled tasks:

  • Periodic scheduled task (crontab command): the specified task is executed every time it reaches the specified time point, and the execution is repeated periodically
  • One time scheduled task (at command): execute a specified task at a certain time in the future

crontab timer

timer

The crontab command allows the user to submit, edit, or delete the corresponding job. Each user can have a crontab file to save scheduling information. You can use it to run any shell script or a command.

crontab command format

Function: used to generate crontab files required by cron process

  • -e edit scheduled tasks
  • -l view scheduled tasks
  • -r delete scheduled tasks
  • -i prompt for deleting scheduled tasks
# crontab -e                                                                         
* * * * * /root/shell/if.sh                                                          

crontab file format

minute hour day-of-month month-of-year day-of-week commands
:----: :--: :----------: :-----------: :---------: :--------:
Minute hour day month week command or script

  • Minute which minute of an hour [0 ~ 59]
  • Hour which hour of the day [0 ~ 23]
  • Day of month [1 ~ 31]
  • Month of year [1 ~ 12]
  • Day of week [0 ~ 6] 0 means Sunday
  • commands command or script to execute

Notes for writing

  1. All cannot be empty and must be filled in. Unknown values use wildcard * to represent any time
  2. Each time field can specify multiple values. Discontinuous values are comma, interval, and continuous values are interval
  3. The command should give an absolute path
  4. The user must have the permission to run the corresponding command or program

Note: where is command: you can view the file path where the command is located

at scheduled task

Install at command

At scheduled task is to specify a time to execute a task, which can only be executed once, and the at command needs to be installed;

[root@node-01 ~]# yum -y install at                                                  
at command format
at [option] TIME                                                                     
                                                                                                                                                                                                                                                            
Start at service
[root@node-01 ~]# systemctl start atd                                                
[root@node-01 ~]# systemctl enable atd                                               
at create task

All tasks created with the at command are saved in a file format under the / var/spool/at / directory

  • Method 1: configure scheduled tasks through CLI
  1. After directly using the at command to specify the time, you will enter a CLI interface, manually enter the operations to be performed at the specified time (you can always enter to specify multiple operations), and finally use Ctrl+D to submit and exit
[root@node-01 ~]# at 21:25                                                           
at> echo "hello,world"                                                               
at> <EOT>                                                                            
job 1 at Tue Mar  9 21:25:00 2021                                                    
  1. Use the at -l or atq command to view the task you just created
[root@node-01 ~]# at -l                                                              
2       Tue Mar  9 21:27:00 2021 a root                                              
[root@node-01 ~]# atq                                                                
2       Tue Mar  9 21:27:00 2021 a root                                              
  1. View task execution results

The corresponding users will be notified of the execution results of at scheduled tasks by mail, so you can directly view the mail contents of the corresponding user files in the / var/spool/mail / directory or use the mail command to specify the number

[root@node-01]# cat /var/spool/mail/root                                             
From root@node-01.localdomain  Tue Mar  9 21:25:01 2021                              
Return-Path: <root@node-01.localdomain>                                              
X-Original-To: root                                                                  
Delivered-To: root@node-01.localdomain                                               
Received: by node-01.localdomain (Postfix, from userid 0)                            
id EC49F200F3D3; Tue,  9 Mar 2021 21:25:00 +0800 (CST)                               
Subject: Output from your job        1                                               
To: root@node-01.localdomain                                                         
Message-Id: <20210309132500.EC49F200F3D3@node-01.localdomain>                        
Date: Tue,  9 Mar 2021 21:25:00 +0800 (CST)                                          
From: root@node-01.localdomain (root)                                                
                                                                                     
hello,world # Scheduled task execution results                                                               
  • Method 2: create a scheduled task by reading the task list in the file
  1. Use the at -f command to read from the specified file and create a task
[root@node-01 ~]# echo "hello" > > at.txt                                               
[root@node-01 ~]# at -f at.txt 21:38                                                 
job 3 at Tue Mar  9 21:38:00 2021                                                    
  1. Use the at -d command to delete the task with the specified number
[root@node-01 spool]# atq                                                            
3       Tue Mar  9 21:38:00 2021 a root                                              
[root@node-01 spool]# at -d 3                                                        
[root@node-01 spool]# atq                                                            
  • Method 3: create a planned task by displaying the content to be executed through echo
[root@node-01 spool]# echo "hello,wolrd"  at 21:40                                  
job 4 at Tue Mar  9 21:40:00 2021                                                    
at common time format
  1. Specify the specific date and time to execute
[root@node-01 ~]# at -f test.txt 20:20 2018-09-05                                    
  1. Specify 10 minutes to execute
[root@node-01 ~]# at -f test.txt now+10minutes                                       
  1. Execute after 1 hour
[root@node-01 ~]# at -f test.txt now+1hours                                          
  1. Execute after 10 days
[root@node-01 ~]# at -f test.txt now+10day                                           

Topics: Linux