Linux 篇:Linux定时任务

什么是crond?

crond是linux用来定期执行命令或指定程序任务的一种服务。安装完操作系统后,默认会启动crond任务调度服务。crond服务会定期检查系统中是否有要执行的任务。如果有要执行的任务便会自动执行该任务。crond定时任务服务就像我们早上使用的闹钟一样,crontab需要启动一个服务crond才行,crond服务通过crontab命令实现。

查看crond服务状态:

命令:service crond status systemctl status crond

启动crond服务:

命令:service crond start systemctl start crond

什么是crontab?

crontab是一个可以在crond服务上添加或编辑定时任务的命令。

设置定时任务命令:

crontab -e -u 用户名 //设定某个用户的crond服务,一般root用户需要这个参数

crontab -l //列出某个用户crond服务的详细内容

crontab -r //删除某个用户的crond服务

crontab -e //编辑某个用户的crond服务

例:root用户要查看自己的详细crond服务内容

命令:crontab -u root -l

而linux任务调度的工作主要分为以下两类:

1、系统执行的工作:系统周期性所要执行的工作,如备份系统数据、清理缓存

2、个人执行的工作:某个用户定期要做的工作,例如每隔10分钟检查邮件服务器是否有新信,这些工作可由每个用户自行设置。

分钟 小时 天数 月数 周数

minute hour day month week

每个字段代表的含义如下:

minute: 表示分钟,可以是从0到59之间的任何整数。

hour:表示小时,可以是从0到23之间的任何整数。

day:表示日期,可以是从1到31之间的任何整数。

month:表示月份,可以是从1到12之间的任何整数。

week:表示星期几,可以是从0到7之间的任何整数,这里的0或7代表星期日。

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... ```

猜你喜欢

转载自blog.csdn.net/zhiqi_l163991102/article/details/131285378
今日推荐