Q:Linux下,如果想要在指定的某个时间点干点什么事,该如何制定计划?
A:
计划任务,创建和管理在指定时间自动执行的任务。
注意:
要是任务计划在指定时间自动运行,计划任务的服务必须是启动的。
1、使用at命令调用atd进程设置在某个特定的时间,执行一次性任务。
2、使用crontab命令调用crond进程,设置按固定的周期(如每天、每周等)重复执行预先计划好的任务。
一、一次性计划任务
在指定的日期、时间点自动执行预先设置的一些命令操作。
系统服务的名称:/etc/init.d/atd
1 2 3 4 5 6 7 8 9 10 |
<span style="font-size: 16px;">[root@localhost ~<span style="color: rgba(0, 0, 0, 1);">]# service atd start [root@localhost </span>~<span style="color: rgba(0, 0, 0, 1);">]# service atd status atd (pid </span><span style="color: rgba(128, 0, 128, 1);">5790</span>) <span style="color: rgba(0, 0, 255, 1);">is</span> running... <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">确保atd系统服务启动 </span> [root@studying mydata]# at <span style="color: rgba(128, 0, 128, 1);">08</span>:<span style="color: rgba(128, 0, 128, 1);">45</span><span style="color: rgba(0, 0, 0, 1);"> at</span>><span style="color: rgba(0, 0, 0, 1);"> reboot <span style="color: rgba(0, 128, 0, 1);">//按Ctrl+D键提交任务</span> at</span>> <EOT><span style="color: rgba(0, 0, 0, 1);"> job </span><span style="color: rgba(128, 0, 128, 1);">3</span> at <span style="color: rgba(128, 0, 128, 1);">2017</span>-<span style="color: rgba(128, 0, 128, 1);">06</span>-<span style="color: rgba(128, 0, 128, 1);">09</span> <span style="color: rgba(128, 0, 128, 1);">08</span>:<span style="color: rgba(128, 0, 128, 1);">45</span><span style="color: rgba(0, 0, 0, 1);"> [root@studying mydata]# atq </span><span style="color: rgba(128, 0, 128, 1);">3</span> <span style="color: rgba(128, 0, 128, 1);">2017</span>-<span style="color: rgba(128, 0, 128, 1);">06</span>-<span style="color: rgba(128, 0, 128, 1);">09</span> <span style="color: rgba(128, 0, 128, 1);">08</span>:<span style="color: rgba(128, 0, 128, 1);">45</span> a root</span> |
1、设置格式
at [HH:MM] [yyyy-mm-dd]
若没有指定年月日的,默认是当天的。
1、at [HH:MM]
2、at [HH:MM] [yyyy-mm-dd]
3、at now + 数字 [minutes|hours|days|weeks]
2、查询当前设置的at任务列表
shell> atq 或at -l
3、删除指定任务编号的at任务
shell> atrm 编号 #atrm=at -d
4、查看计划任务的具体内容
shell> at -c 任务号
5、使用文件中的命令作为计划任务(比较少用)
shell> at 时间 -f 文件
1、at计划任务文件:/var/spool/at/a开头的文件,任务执行完毕,文件消失。
2、/etc/at.deny:只有该名单里面的人不可以使用
3、/etc/at.allow:只有该名单里面的人可以使用
二、周期计划任务
按照预先设置的时间周期(分钟、小时、天……)重复执行用户指定的命令操作。
1、crontab命令调用crond进程执行。
2、系统服务的名称:/etc/init.d/crond
3、用户定义的设置,位于文件:/var/spool/cron/用户名
4、cron服务的日志文件:/var/log/cron
[root@localhost /]# service crond start
[root@localhost /]# service crond status
crond (pid 1935) is running…
1、cron服务配置文件:/etc/crontab
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span style="font-size: 16px;">[root@studying mydata]# cat /etc/<span style="color: rgba(0, 0, 0, 1);">crontab SHELL</span>=/bin/<span style="color: rgba(0, 0, 0, 1);">bash PATH</span>=/sbin:/bin:/usr/sbin:/usr/<span style="color: rgba(0, 0, 0, 1);">bin MAILTO</span>=<span style="color: rgba(0, 0, 0, 1);">root HOME</span>=/<span style="color: rgba(0, 0, 0, 1);"> # For details see man </span><span style="color: rgba(128, 0, 128, 1);">4</span><span style="color: rgba(0, 0, 0, 1);"> crontabs # Example of job definition: # .</span>---------------- minute (<span style="color: rgba(128, 0, 128, 1);">0</span> - <span style="color: rgba(128, 0, 128, 1);">59</span><span style="color: rgba(0, 0, 0, 1);">) # </span>| .------------- hour (<span style="color: rgba(128, 0, 128, 1);">0</span> - <span style="color: rgba(128, 0, 128, 1);">23</span><span style="color: rgba(0, 0, 0, 1);">) # </span>| | .---------- day of month (<span style="color: rgba(128, 0, 128, 1);">1</span> - <span style="color: rgba(128, 0, 128, 1);">31</span><span style="color: rgba(0, 0, 0, 1);">) # </span>| | | .------- month (<span style="color: rgba(128, 0, 128, 1);">1</span> - <span style="color: rgba(128, 0, 128, 1);">12</span><span style="color: rgba(0, 0, 0, 1);">) OR jan,feb,mar,apr ... # </span>| | | | .---- day of week (<span style="color: rgba(128, 0, 128, 1);">0</span> - <span style="color: rgba(128, 0, 128, 1);">6</span>) (Sunday=<span style="color: rgba(128, 0, 128, 1);">0</span> or <span style="color: rgba(128, 0, 128, 1);">7</span><span style="color: rgba(0, 0, 0, 1);">) OR sun,mon,tue,wed,thu,fri,sat # </span>| | | | |<span style="color: rgba(0, 0, 0, 1);"> # </span>* * * * * user-name command to be executed</span> |
注意:今后写脚本让crontab去自动执行的时候,尽量写命令的绝对目录。
crontab 只会在/sbin、/bin、/usr/sbin、/usr/bin下查找命令,如果命令不在这些目录中,应该要使用绝对路径。
2、管理cron计划任务
root用户可以管理指定用户的计划任务、普通用户只能管理自己的计划任务
1、编辑计划任务:crontab -e -u 用户名
2、查看计划任务:crontab -l -u 用户名
3、删除计划任务:crontab -r -u 用户名
1 2 3 4 5 6 7 8 9 |
<span style="font-size: 16px;">[root@localhost lianxi]# crontab -e <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">进入编辑模式,不接用户名的,默认是当前用户</span><span style="color: rgba(0, 0, 0, 1);"> no crontab </span><span style="color: rgba(0, 0, 255, 1);">for</span> root - <span style="color: rgba(0, 0, 255, 1);">using</span><span style="color: rgba(0, 0, 0, 1);"> an empty one crontab: installing </span><span style="color: rgba(0, 0, 255, 1);">new</span><span style="color: rgba(0, 0, 0, 1);"> crontab [root@localhost lianxi]# crontab </span>-<span style="color: rgba(0, 0, 0, 1);">l </span><span style="color: rgba(128, 0, 128, 1);">50</span> <span style="color: rgba(128, 0, 128, 1);">3</span> <span style="color: rgba(128, 0, 128, 1);">2</span> <span style="color: rgba(128, 0, 128, 1);">3</span> * echo <span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">123</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> [root@localhost lianxi]# crontab </span>-<span style="color: rgba(0, 0, 0, 1);">r [root@localhost lianxi]# crontab </span>-<span style="color: rgba(0, 0, 0, 1);">l no crontab </span><span style="color: rgba(0, 0, 255, 1);">for</span> root</span> |
3、编辑语法
通过crontab -e 进入编辑模式后,语法格式如下:
时间数值的特殊表示方法:
1、* 表示该范围内的任意时间
2、, 表示间隔的多个不连续时间点
3、- 表示一个连续的时间范围
4、/n 指定间隔的时间频率
1 2 3 4 |
<span style="font-size: 16px;"><span style="color: rgba(128, 0, 128, 1);">0</span> <span style="color: rgba(128, 0, 128, 1);">17</span> * * <span style="color: rgba(128, 0, 128, 1);">1</span>- <span style="color: rgba(128, 0, 128, 1);">5</span> <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">周一到周五每天17:00</span> <span style="color: rgba(128, 0, 128, 1);">30</span> <span style="color: rgba(128, 0, 128, 1);">8</span> * * <span style="color: rgba(128, 0, 128, 1);">1</span>,<span style="color: rgba(128, 0, 128, 1);">3</span>,<span style="color: rgba(128, 0, 128, 1);">5</span> <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">每周一、三、五的8点30分</span> <span style="color: rgba(128, 0, 128, 1);">0</span> <span style="color: rgba(128, 0, 128, 1);">8</span>-<span style="color: rgba(128, 0, 128, 1);">18</span> * * * <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">8点到18点整</span> <span style="color: rgba(128, 0, 128, 1);">0</span> <span style="color: rgba(128, 0, 128, 1);">12</span> */<span style="color: rgba(128, 0, 128, 1);">3</span> * * <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">每隔3天的12点整</span></span> |
注意:
1、资源分配不均:多个计划任务同时运行。
2、安全检查:防止有非法计划任务。
3、周和日、月不可同时并存:容易导致计划任务时间混乱。
4、缺陷弥补
缺陷:cron在系统关机后不能执行计划任务……
为周期性计划任务cron收拾残局的anacron程序解决该缺陷。
1、anacron程序是系统自带的定时任务,不能替代cron
服务名称:/etc/init.d/anacrond
服务日志:/var/spool/anacron
服务配置文件:/etc/anacrontab
2、会每个小时被执行一次,脚本在/etc/cron.hourly
3、按天、周或月为单位去检查系统未进行的cron任务
4、开机时自动运行,然后将未执行的计划任务执行一遍后,anacron 就会自动停止