Linux article: Linux scheduled tasks

What is crond?

crond is a service used by Linux to regularly execute commands or specify program tasks. After installing the operating system, the crond task scheduling service will be started by default. The crond service regularly checks whether there are tasks to be performed in the system. If there is a task to be executed, the task will be executed automatically. The crond scheduled task service is just like the alarm clock we use in the morning. Crontab needs to start a service crond. The crond service is implemented through the crontab command.

Check the crond service status:

命令:service crond status systemctl status crond

Start the crond service:

命令:service crond start systemctl start crond

What is a crontab?

crontab is a command that can add or edit scheduled tasks on the crond service.

Set scheduled task command:

crontab -e -u username//Set the crond service of a certain user. Generally, root users need this parameter.

crontab -l //List the details of a user's crond service

crontab -r //Delete a user's crond service

crontab -e //Edit a user's crond service

Example: The root user wants to view his detailed crond service content

Instruction: crontab -u root -l

Linux task scheduling work is mainly divided into the following two categories:

1. Work performed by the system: Work performed periodically by the system, such as backing up system data and cleaning cache

2. Work performed by individuals: tasks that a user must do regularly, such as checking the mail server every 10 minutes to see if there are new messages. These tasks can be set by each user.

Minutes, hours, days, months, weeks

minute hour day month week

The meaning of each field is as follows:

minute: represents the minute, which can be any integer from 0 to 59.

hour: represents the hour, which can be any integer from 0 to 23.

day: represents the date, which can be any integer from 1 to 31.

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

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

command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件。

在以上各个字段中,还可以使用以下特殊字符:

星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。

逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”

中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”

正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。

注意:在 crontab 命令中只有 “绝对路径”,不存在相对路径,故执行任何命令都需要写绝对路径

1、每小时的第5分钟执行 /usr/bin/touch /tmp/testfile.txt 命令

5 * * * * /usr/bin/touch /tmp/testfile.txt

2、每5分钟执行 /usr/bin/touch /tmp/testfile.txt 命令

*/5 * * * * /usr/bin/touch /tmp/testfile.txt

3、每天的 4:30 执行 /usr/bin/touch /tmp/testfile.txt 命令

30 4 * * * /usr/bin/touch /tmp/testfile.txt

4、每小时执行 /usr/bin/touch /tmp/testfile.txt 命令

0 * * * * /usr/bin/touch /tmp/testfile.txt

5、每天执行 /usr/bin/touch /tmp/testfile.txt 命令

0 0 * * * /usr/bin/touch /tmp/testfile.txt

6、每周执行 /usr/bin/touch /tmp/testfile.txt 命令

0 0 * * 0 /usr/bin/touch /tmp/testfile.txt

7、每年执行 /usr/bin/touch /tmp/testfile.txt 命令

0 0 1 1 * /usr/bin/touch /tmp/testfile.txt

8、每月 8号 的 7:20 执行 /usr/bin/touch /tmp/testfile.txt 命令

20 7 8 * * /usr/bin/touch /tmp/testfile.txt

9、每年的 6月28号 5:30 执行 /usr/bin/touch /tmp/testfile.txt 命令

30 5 28 6 * /usr/bin/touch /tmp/testfile.txt

10、每星期日的 6:30 执行 /usr/bin/touch /tmp/testfile.txt 命令

30 6 * * 0 /usr/bin/touch /tmp/testfile.txt

注意:0 表示星期天, 1 表示星期一,以此类推;也可以用英文来表示,sun 表示星期天,mon 表示星期一等。

11、每月 10号和20号 的 4:30 执行 /usr/bin/touch /tmp/testfile.txt 命令

30 4 10,20 * * /usr/bin/touch /tmp/testfile.txt

注意:" , " 用来连接多个不连续的时间

12、每天 8~11点 的第 25 分钟执行 /usr/bin/touch /tmp/testfile.txt 命令

25 8-11 * * * /usr/bin/touch /tmp/testfile.txt

注意:" - " 用来连接连续的时间

13、每个月中每隔 10天 的 5:30 执行 /usr/bin/touch /tmp/testfile.txt 命令

30 5 */10 * * /usr/bin/touch /tmp/testfile.txt

即:每月的 1、11、21、31日 在 5:30 执行一次 /usr/bin/touch /tmp/testfile.txt 命令

at命令 一次性定时计划任务

at命令允许指定运行脚本时间,at的守护进程atd会以后台模式运行,检查系统上的一个特殊目录来获取at命令的提交的作业。默认情况下,atd守护进程每60秒检查一次目录。有作业时会检查作业运行时间,如果与当前时间匹配,则运行此作业。

语法格式:at [参数]

常用参数:

img

``` 参考实例 ​ 查看系统中的等待作业: [root@at ~]# atq ​ 使用”at -d”或者”atrm”(二者同效)指定id来删除系统中的等待作业,id为”atq”命令输出的第一行顺序数字: [root@at ~]# at -d 1 [root@at ~]# atrm 1 ​ 让 at.sh 脚本立即运行: [root@at ~]# at -f /root/at.sh now ​ 在25分钟之后运行 at.sh 脚本: [root@at ~]# at -f /root/at.sh now +25 min ​ 在10:11运行 at.sh 脚本: [root@at ~]# at -f /root/at.sh 10:11 ​ 在2019年7月27日运行 at.sh 脚本: [root@at ~]# at -f /root/at.sh 07/27/2019 ​ [root@Jaking11 ~]#ls HelloWorld1.sh HelloWorld2.sh HelloWorld3.sh httpd.sh test1.txt [root@Jaking11 ~]#date Sat Apr  4 11:13:53 CST 2020 [root@Jaking11 ~]#at -f HelloWorld1.sh 10:15 job 1 at Sun Apr  5 10:15:00 2020 Can't open /var/run/atd.pid to signal atd. No atd running? [root@Jaking11 ~]#atq 1       Sun Apr  5 10:15:00 2020 a root [root@Jaking11 ~]#date Sat Apr  4 11:14:52 CST 2020 [root@Jaking11 ~]#date Sat Apr  4 11:15:16 CST 2020 ​ 补充: [root@Jaking11 ~]#at -f HelloWorld3.sh 14:47 04/08/2020     job 13 at Wed Apr  8 14:47:00 2020 ​ 运行脚本 [root@Jaking11 tmp]#vim test.sh

!/bin/bash

touch /tmp/testfile.txt [root@Jaking11 tmp]#ls test.sh [root@Jaking11 tmp]#chmod 755 test.sh [root@Jaking11 tmp]#date Sat Apr  4 11:32:14 CST 2020 [root@Jaking11 tmp]#at -f /tmp/test.sh 11:33 job 8 at Sat Apr  4 11:33:00 2020 [root@Jaking11 tmp]#date Sat Apr  4 11:32:30 CST 2020 [root@Jaking11 tmp]#ls test.sh [root@Jaking11 tmp]#date Sat Apr  4 11:33:00 CST 2020 [root@Jaking11 tmp]#ls testfile.txt test.sh ​ 运行命令 [root@Jaking11 tmp]#at now at> echo "hello world" >> at.out at>  # 按 Ctrl + D 提交任务 job 10 at Sat Apr  4 11:42:00 2020 [root@Jaking11 tmp]# [root@Jaking11 tmp]#ls at.out testfile.txt test.sh [root@Jaking11 tmp]#cat at.out hello world [root@Jaking11 tmp]#at now +1 minutes at> echo date >> at.out at> job 11 at Sat Apr  4 11:44:00 2020 You have new mail in /var/spool/mail/root [root@Jaking11 tmp]# [root@Jaking11 tmp]#cat at.out hello world Sat Apr 4 11:44:00 CST 2020 [root@Jaking11 tmp]#cat at.out hello world Sat Apr 4 11:44:00 CST 2020 ​ 解决 -bash: at: command not found

[root@Jaking11 ~]#at -bash: at: command not found

反查,at命令是由哪个包提供的

[root@Jaking11 ~]#yum provides at Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. at-3.1.13-22.el7.x86_64 : Job spooling too/usr/bin/touch /tmp/testfile.txt Repo       : yum

安装对应的包

[root@Jaking11 ~]#yum install -y at-3.1.13-22.el7.x8664 Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package at.x8664 0:3.1.13-22.el7 will be installed --> Finished Dependency Resolution

Dependencies Resolved

=======================================================================

Package     Arch           Version               Repository   Size

Installing: at         x86_64          3.1.13-22.el7         yum           51 k

Transaction Summary

Install  1 Package

Total download size: 51 k Installed size: 95 k Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : at-3.1.13-22.el7.x8664                             1/1 Verifying : at-3.1.13-22.el7.x8664                             1/1

Installed: at.x86_64 0:3.1.13-22.el7                                            

Complete! [root@Jaking11 ~]#systemctl start atd [root@Jaking11 ~]#systemctl status atd ● atd.service - Job spooling too/usr/bin/touch /tmp/testfile.txt   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)   Active: active (running) since Sat 2020-04-04 11:16:26 CST; 12min ago Main PID: 11908 (atd)   CGroup: /system.slice/atd.service           └─11908 /usr/sbin/atd -f ​ Apr 04 11:16:26 Jaking11 systemd[1]: Started Job spooling too/usr/bin/touch /tmp/testfile.txt. Apr 04 11:16:26 Jaking11 systemd[1]: Starting Job spooling too/usr/bin/touch /tmp/testfile.txt... ```

Guess you like

Origin blog.csdn.net/zhiqi_l163991102/article/details/131285378