Article directory
One-time scheduling execution at
Cyclic scheduling to execute crontab commands
Check the service (whether it is started)
Grammar formatting in the editor
Interpretation of task information involved in the code
foreword
I have learned the software management in Linux before. This article will start to learn the scheduled tasks in Linux. Through this article, I will learn the introduction and use of the one-time scheduling execution of the at command and the cyclic scheduling execution of the cron command in Linux. Let's start Learn about Linux scheduled tasks.
What is a scheduled task?
Scheduling tasks is a common function of the system. Using the task scheduling function, any script, program or document can be scheduled to run at a certain most convenient time. The task scheduler starts every time the system starts and runs in the background. When we need to execute some repetitive events on the server regularly, we can use the scheduled task program to run prepared scripts , batch folders, programs or commands, and run them at a specific time. Currently, the most important The purpose is to back up data regularly. .
One-time scheduling execution at
Introduction
Some scenarios we only need to execute once, and we use the at command to end the task after execution, and the execution log of the task is fed back to the computer in the form of email.
service start
All linux systems provide this feature by default. In redhat, atd is started with the system startup by default.
[root@localhost ~]# ps -ef |grep atd
root 1284 1 0 06:25 ? 00:00:00 /usr/sbin/atd -f
root 12970 12205 0 10:51 pts/0 00:00:00 grep --color=auto atd
您在 /var/spool/mail/root 中有新邮件
[root@localhost ~]# systemctl status atd.service
● atd.service - Job spooling tools
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: active (running) since 三 2022-11-02 15:48:16 CST; 4 days ago
Main PID: 1284 (atd)
Tasks: 1
CGroup: /system.slice/atd.service
└─1284 /usr/sbin/atd -f
11月 02 15:48:16 localhost.localdomain systemd[1]: Started Job spooling tools.
11月 07 10:40:00 localhost.localdomain atd[12686]: Starting job 1 (a0000101a82400)...)
11月 07 10:44:00 localhost.localdomain atd[12858]: Starting job 2 (a0000201a82404)...)
Hint: Some lines were ellipsized, use -l to show in full.
grammar
at -参数 <TIMESPEC>
parameter:
- -m When the specified task is completed, send mail to the user, even if there is no standard output
- Alias for -I atq
- Alias for -d atrm
- -v displays the time 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 tasks from specified file instead of standard input
- -t<time parameter> Submit the task to run in the form of time parameter
example
[root@localhost ~]# at -v
Garbled time
[root@localhost ~]# at -V
at version 3.1.13
Submit at job
submission steps
- Enter the at command and specify the execution time of the job, and press Enter;
- At the command prompt "at >", enter the command or shell script
- Ctrl+D Submit at job.
Note: If you want to execute multiple lines of commands, after entering a command, press Enter and enter the next one until you press Ctrl+D to submit the task.
time selection format
now +5min // add 5 minutes to the current time
Time tomorrow (teatime is 16:00) // execute at a certain time tomorrow
5pm august 3 2029 //executed at 5pm on August 3, 2029
4:00 2019-11-27 //executed at 4:00 on November 27, 2019
example
[root@localhost ~]# date
2022年 11月 07日 星期一 11:00:00 CST
[root@localhost ~]# at now +5min
at> ls
at> <EOT>
job 3 at Mon Nov 7 11:05:00 2022
[root@localhost ~]# at teatime tomorrow
at> ls /test
at> <EOT>
job 4 at Tue Nov 8 16:00:00 2022
[root@localhost ~]# at 8:00 tomorrow
at> ls /test
at> <EOT>
job 5 at Tue Nov 8 08:00:00 2022
at> useradd sure2023
at> <EOT>
job 6 at Thu Aug 3 17:00:00 2023
[root@localhost ~]# at 5:20 2023-5-20
at> touch /test/love.txt
at> echo "I Love You">> /test/love.txt
at> <EOT>
job 8 at Sat May 20 05:20:00 2023
view tasks
Command: atq
example
[root@localhost ~]# date
2022年 11月 07日 星期一 11:08:25 CST
[root@localhost ~]# atq
4 Tue Nov 8 16:00:00 2022 a root
5 Tue Nov 8 08:00:00 2022 a root
6 Thu Aug 3 17:00:00 2023 a root
8 Sat May 20 05:20:00 2023 a root
The time has passed 11:05, so task No. 3 has been executed and cannot be found in the plan list.
View task execution logs
The task execution log exists in the form of mail, and the location is /var/spool/mail/root.
view via mali
example
[root@localhost ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 3 messages
> 1 root Mon Nov 7 10:40 15/514 "Output from your job "
2 root Mon Nov 7 10:44 24/601 "Output from your job "
3 root Mon Nov 7 11:05 15/516 "Output from your job "
& 2
Message 2:
From [email protected] Mon Nov 7 10:44:01 2022
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
Subject: Output from your job 2
To: [email protected]
Date: Mon, 7 Nov 2022 10:44:01 +0800 (CST)
From: [email protected] (root)
Status: RO
anaconda-ks.cfg
initial-setup-ks.cfg
公共
模板
视频
图片
文档
下载
音乐
桌面
& q
Held 3 messages in /var/spool/mail/root
delete task
Command: atrm task number or atrm file name
Or use the alias of atrm: at -d
example
[root@localhost ~]# atq
4 Tue Nov 8 16:00:00 2022 a root
5 Tue Nov 8 08:00:00 2022 a root
6 Thu Aug 3 17:00:00 2023 a root
8 Sat May 20 05:20:00 2023 a root
[root@localhost ~]# atrm 4
[root@localhost ~]# atq
5 Tue Nov 8 08:00:00 2022 a root
6 Thu Aug 3 17:00:00 2023 a root
8 Sat May 20 05:20:00 2023 a root
Mission #4 has been removed.
Cyclic scheduling to execute crontab commands
Introduction
The concept of cron and crontab are inseparable;
crontab is a command commonly found in Unix and Linux operating systems;
Used to set periodically executed instructions;
This command reads instructions from the standard input device and stores them in the "crontab" file for later reading and execution.
Scheduled task storage location: /var/spool/cron/
Check the service (whether it is started)
[root@localhost ~]# ps -ef |grep cron
root 1288 1 0 06:25 ? 00:00:00 /usr/sbin/crond -n
root 16413 12205 0 11:23 pts/0 00:00:00 grep --color=auto cron
[root@localhost ~]# systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 三 2022-11-02 15:48:16 CST; 4 days ago
Main PID: 1288 (crond)
Tasks: 1
CGroup: /system.slice/crond.service
└─1288 /usr/sbin/crond -n
11月 02 15:48:16 localhost.localdomain systemd[1]: Started Command Scheduler.
11月 02 15:48:16 localhost.localdomain crond[1288]: (CRON) INFO (RANDOM_DELAY will...)
11月 02 15:48:18 localhost.localdomain crond[1288]: (CRON) INFO (running with inot...)
Hint: Some lines were ellipsized, use -l to show in full.
If the service is not started, just use the command systemctl start to start the service name. The cron program is the basis for the execution of the scheduled task.
create plan
Command: crontab -e //Edit the current user, except for its own user and root, other users cannot find it.
Grammar formatting in the editor
* * * * * (representing time) command
minute hour day month week
min range: 1---59
hours range: 1---23
day range: 1---31
month range: 1---12
week range: 0---6 (Sunday=0)Time-sharing day-month-week command or script program
Leave a part separated by a space, and * means that the station symbol needs to have * even if there is no content to be set there.
example
[root@localhost ~]# crontab -e
*/5 * * * * echo "hello ">> /test/hello.txt
0 2 1,4,6 * * ls /
0 2 5-9 * * ls /
0 * * * * ls /
0 2 * * * ls /
0 2 14 * * ls /
20 5 20 5 * echo "i love you" >> /test/love.txt
0 2 * * 5 ls /
0 2 2 6 5 ls /
Interpretation of task information involved in the code
*/5 * * * * echo "hello ">> /test/hello.txt //Execute every five minutes
0 2 1,4,6 * * ls //1st, 4th, 6th of each month Execute once at 2:00
0 2 5-9 * * ls / / Execute once at 2:00 from the 5th to the 9th of each month
0 * * * * ls / / Execute once at the 0th minute of every hour
0 2 * * * ls / / Execute once at 2:00 every day
0 2 14 * * ls / / Execute once at 2:00 on the 14th of each month
20 5 20 5 * echo "i love you" >> /test/ love.txt //Execute once at 5:20 on May 20 every year
0 2 * * 5 ls //Execute once at 2:00 on Friday every month
0 2 2 6 5 ls //June every year Executed at 2:00 on the 2nd and at 2:00 on Friday in June every year
query plan
Command: crontab -l
Administrators can use -u username to manage scheduled tasks of other users
View plan execution log command: mail
[root@localhost ~]# crontab -l
*/5 * * * * echo "hello ">> /test/hello.txt
0 2 1,4,6 * * ls /
0 2 5-9 * * ls /
0 * * * * ls /
0 2 * * * ls /
0 2 14 * * ls /
20 5 20 5 * echo "i love you" >> /test/love.txt
0 2 * * 5 ls /
0 2 2 6 5 ls /
您在 /var/spool/mail/root 中有邮件
Every five minutes, hello will be automatically written to hello.txt, indicating that the task plan is set successfully. Generally, we will use the operation of executing tasks cyclically as a backup in the production environment.
delete plan
Command: crontab -r deletes the current user's plan.
[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root
Summarize
When backing up data, we usually use the crontab command. When we only need to execute it once at a specified time, we use the at command to complete it. In this article, we understand the task plan management in Linux and learn to use at and crontab commands. and their application scenarios.
It's not easy to create, please give me a like and follow with your little hands, tell me in the comment area if you have any comments, and learn together.