哪些是crond?
crond是linux拿来定期执行命令或指定程序任务的一种服务。安装完操作系统后,默认会启动crond任务调度服务。crond服务会定期检测系统中是否有要执行的任务。假如有要执行的任务便会手动执行该任务。crond定时任务服务如同我们夜里使用的闹铃一样,crontab须要启动一个服务crond才行,crond服务通过crontab命令实现。
查看crond服务状态:
命令:servicecrondstatussystemctlstatuscrond
启动crond服务:
命令:servicecrondstartsystemctlstartcrond
哪些是crontab?
crontab是一个可以在crond服务上添加或编辑定时任务的命令。
设置定时任务命令:
crontab-e-u用户名//设定某个用户的crond服务,通常root用户须要这个参数
crontab-l//列举某个用户crond服务的详尽内容
crontab-r//删掉某个用户的crond服务
crontab-e//编辑某个用户的crond服务
例:root用户要查看自己的详尽crond服务内容
命令:crontab-uroot-l
而linux任务调度的工作主要分为以下两类:
1、系统执行的工作:系统周期性所要执行的工作,如备份系统数据、清理缓存
2、个人执行的工作:某个用户定期要做的工作,比如每隔10分钟检测短信服务器是否有新信,这种工作可由每位用户自行设置。
分钟小时天数月数周数
minutehourdaymonthweek
每位数组代表的涵义如下:
minute:表示分钟,可以是从0到59之间的任何整数。
hour:表示小时,可以是从0到23之间的任何整数。
day:表示日期,可以是从1到31之间的任何整数。
month:表示月份,可以是从1到12之间的任何整数。
week:表示礼拜几redhat linux 9.0,可以是从0到7之间的任何整数,这儿的0或7代表礼拜日。
command:要执行的命令,可以是系统命令,也可以是自己编撰的脚本文件。
在以上各个数组中,还可以使用以下特殊字符:
星号(*):代表所有可能的值,比如month数组假如是星号,则表示在满足其它数组的阻碍条件后每月都执行该命令操作。
冒号(,):可以用冒号隔开的值指定一个列表范围,比如,“1,2,5,7,8,9”
中杠(-):可以用整数之间的中杠表示一个整数范围,比如“2-6”表示“2,3,4,5,6”
正斜线(/):可以用正斜线指定时间的间隔频度红旗linux5.0,比如“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命令
304***/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命令
00***/usr/bin/touch/tmp/testfile.txt
6、每周执行/usr/bin/touch/tmp/testfile.txt命令
00**0/usr/bin/touch/tmp/testfile.txt
7、每年执行/usr/bin/touch/tmp/testfile.txt命令
0011*/usr/bin/touch/tmp/testfile.txt
8、每月8号的7:20执行/usr/bin/touch/tmp/testfile.txt命令
2078**/usr/bin/touch/tmp/testfile.txt
9、每年的6月28号5:30执行/usr/bin/touch/tmp/testfile.txt命令
305286*/usr/bin/touch/tmp/testfile.txt
10、每礼拜日的6:30执行/usr/bin/touch/tmp/testfile.txt命令
306**0/usr/bin/touch/tmp/testfile.txt
注意:0表示礼拜天,1表示礼拜一,以这种推;也可以用中文来表示,sun表示礼拜天,mon表示礼拜一等。
11、每月10号和20号的4:30执行/usr/bin/touch/tmp/testfile.txt命令
30410,20**/usr/bin/touch/tmp/testfile.txt
注意:","拿来联接多个不连续的时间
12、每天8~11点的第25分钟执行/usr/bin/touch/tmp/testfile.txt命令
258-11***/usr/bin/touch/tmp/testfile.txt
注意:"-"拿来联接连续的时间
13、每个月中每隔10天的5:30执行/usr/bin/touch/tmp/testfile.txt命令
305*/10**/usr/bin/touch/tmp/testfile.txt
即:每月的1、11、21、31日在5:30执行一次/usr/bin/touch/tmp/testfile.txt命令
at命令一次性定时计划任务
at命令准许指定运行脚本时间,at的守护进程atd会以后台模式运行,检测系统上的一个特殊目录来获取at命令的递交的作业。默认情况下linux定时执行sql,atd守护进程每60秒检测一次目录。有作业时会检测作业运行时间linux定时执行sql,假如与当前时间匹配,则运行此作业。
句型格式:at[参数]
常用参数:
```参考实例查看系统中的等待作业:[root@at~]#atq使用”at-d”或者”atrm”(两者同效)指定id来删掉系统中的等待作业,id为”atq”命令输出的第一行次序数字:[root@at~]#at-d1[root@at~]#atrm1让at.sh脚本立刻运行:[root@at~]#at-f/root/at.shnow在25分钟以后运行at.sh脚本:[root@at~]#at-f/root/at.shnow+25min在10:11运行at.sh脚本:[root@at~]#at-f/root/at.sh10:11在2019年7月27日运行at.sh脚本:[root@at~]#at-f/root/at.sh07/27/2019[root@Jaking11~]#lsHelloWorld1.shHelloWorld2.shHelloWorld3.shhttpd.shtest1.txt[root@Jaking11~]#dateSatApr411:13:53CST2020[root@Jaking11~]#at-fHelloWorld1.sh10:15job1atSunApr510:15:002020Can'topen/var/run/atd.pidtosignalatd.Noatdrunning?[root@Jaking11~]#atq1SunApr510:15:002020aroot[root@Jaking11~]#dateSatApr411:14:52CST2020[root@Jaking11~]#dateSatApr411:15:16CST2020补充:[root@Jaking11~]#at-fHelloWorld3.sh14:4704/08/2020job13atWedApr814:47:002020运行脚本[root@Jaking11tmp]#vimtest.sh
!/bin/bash
touch/tmp/testfile.txt[root@Jaking11tmp]#lstest.sh[root@Jaking11tmp]#chmod755test.sh[root@Jaking11tmp]#dateSatApr411:32:14CST2020[root@Jaking11tmp]#at-f/tmp/test.sh11:33job8atSatApr411:33:002020[root@Jaking11tmp]#dateSatApr411:32:30CST2020[root@Jaking11tmp]#lstest.sh[root@Jaking11tmp]#dateSatApr411:33:00CST2020[root@Jaking11tmp]#lstestfile.txttest.sh运行命令[root@Jaking11tmp]#atnowat>echo"helloworld">>at.outat>#按Ctrl+D递交任务job10atSatApr411:42:002020[root@Jaking11tmp]#[root@Jaking11tmp]#lsat.outtestfile.txttest.sh[root@Jaking11tmp]#catat.outhelloworld[root@Jaking11tmp]#atnow+1minutesat>echodate>>at.outat>job11atSatApr411:44:002020Youhavenewmailin/var/spool/mail/root[root@Jaking11tmp]#[root@Jaking11tmp]#catat.outhelloworldSatApr411:44:00CST2020[root@Jaking11tmp]#catat.outhelloworldSatApr411:44:00CST2020解决-bash:at:commandnotfound
[root@Jaking11~]#at-bash:at:commandnotfound
反查,at命令是由那个包提供的
[root@Jaking11~]#yumprovidesatLoadedplugins:product-id,search-disabled-repos,subscription-managerThissystemisnotregisteredtoRedHatSubscriptionManagement.Youcanusesubscription-managertoregister.at-3.1.13-22.el7.x86_64:Jobspoolingtoo/usr/bin/touch/tmp/testfile.txtRepo:yum
安装对应的包
[root@Jaking11~]#yuminstall-yat-3.1.13-22.el7.x8664Loadedplugins:product-id,search-disabled-repos,subscription-managerThissystemisnotregisteredtoRedHatSubscriptionManagement.Youcanusesubscription-managertoregister.ResolvingDependencies-->Runningtransactioncheck--->Packageat.x86640:3.1.13-22.el7willbeinstalled-->FinishedDependencyResolution
DependenciesResolved
=======================================================================
PackageArchVersionRepositorySize
Installing:atx86_643.1.13-22.el7yum51k
TransactionSummary
Install1Package
Totaldownloadsize:51kInstalledsize:95kDownloadingpackages:RunningtransactioncheckRunningtransactiontestTransactiontestsucceededRunningtransactionInstalling:at-3.1.13-22.el7.x86641/1Verifying:at-3.1.13-22.el7.x86641/1
Installed:at.x86_640:3.1.13-22.el7
Complete![root@Jaking11~]#systemctlstartatd[root@Jaking11~]#systemctlstatusatd●atd.service-Jobspoolingtoo/usr/bin/touch/tmp/testfile.txtLoaded:loaded(/usr/lib/systemd/system/atd.service;enabled;vendorpreset:enabled)Active:active(running)sinceSat2020-04-0411:16:26CST;12minagoMainPID:11908(atd)CGroup:/system.slice/atd.service└─11908/usr/sbin/atd-fApr0411:16:26Jaking11systemd[1]:StartedJobspoolingtoo/usr/bin/touch/tmp/testfile.txt.Apr0411:16:26Jaking11systemd[1]:StartingJobspoolingtoo/usr/bin/touch/tmp/testfile.txt...```