Linux任务计划

任务计划:

                   在未来时间点一次性执行某任务:at, batch

                   周期性执行某任务: crontab

at

 

                   承载未来时间运行的某作业:

 

                   支持使用作业队列:

                            默认为a队列;

                   Ctrl+d保存任务

                   at [option]... TIME

                   TIME:

                            (1) 绝对时间

                                     HH:MM,

                                     MMDD[CC]YY,MM/DD/[CC]YY, DD.MM.[CC]YY or [CC]YY-MM-DD

                                     tomorrow

                            (2) 相对时间

                                     now+#UNIT

                                               minute,hour, day, week

                            (3) 模糊时间

                                     Midnight 午夜

                                     Noon下午

                                     Teatime 下午茶时间

                   常用选项:

                            -q QUEUE at作业队列;

                            -f/PATH/FROM/SOMEFILE:从指定的文件中读取要运行的作业;

                            -l: 查看作业队列中的等运行作业列表;相当于使用atq命令;

                            -c AT_JOB_NUM: 查看待运行作业的内容;

                            -d: 删除指定作业;相当于atrm

batch:

                   由系统自行选择在资源较为空闲时运行指定的任务;

crontab:周期性任务计划

                   守护进程:crond

 

                   周期性任务有两类:

                            (1) 系统cron任务;没有默认运行用户身份,所以需要额外指定运行者;

                                     /etc/crontab

                                     vim命令

                                               #Example of job definition:

                                               #.---------------- minute (0 - 59)

                                               #|  .------------- hour (0 - 23)

                                               #|  | .---------- day of month (1 - 31)

                                               #|  | |  .------- month (1 - 12) ORjan,feb,mar,apr ...

                                               #|  | |  |  .---- day of week (0 - 6) (Sunday=0 or 7) ORsun,mon,tue,wed,thu,fri,sat

                                               #|  | |  |  |

                                               #*  * *  *  * user-name command to be executed    

 

                                               7个字段:

                                                        5个字段:时间点

                                                        user-name:user-name运行

                                                        commandto be executed: 要运行任务                           

 

                            (2) 用户cron任务:由某个用户所提交,默认就以提交者的身份运行,所以无需额外指定运行者;

                                     /var/spool/cron/USERNAME

 

                                     vim命令;不建议

                                     crontab命令:建议

 

                                               #Example of job definition:

                                               #.---------------- minute (0 - 59)

                                               #|  .------------- hour (0 - 23)

                                               #|  | .---------- day of month (1 - 31)

                                               #|  | |  .------- month (1 - 12) ORjan,feb,mar,apr ...

                                               #|  | |  |  .---- day of week (0 - 6) (Sunday=0 or 7) ORsun,mon,tue,wed,thu,fri,sat

                                               #|  | |  |  |

                                               #*  * *  *  * command to be executed      

                                               5个时间点:

                                                        分钟:有效取值范围0-59

                                                        小时:0-23

                                                        日:1-31

                                                        月:1-12

                                                        周几:0-7

                                                        注意:几月几号与周几,不建议同时使用;

                                                        时间表示法:

                                                                 *:对应时间点有效取值范围内的每个时间点;

                                                                 -:一个特定连续时间范围, 3-7

                                                                 ,:一个离散的时间点, 3,5,7

                                                                 /#:有效时间范围内的每多少时间;用于指定频率;

                                                                           1-30/4,*/4

 

                                                        5*/3 * * * /bin/echo "howdy"

 

                                                        57 * * 1-5 /bin/echo "howdy"

 

                                               crontab命令:

                                                        crontab[-u user] [-l | -r | -e]

                                                                 -uuser: 不是管理自己的cron任务,而是指定的目标用户的cron任务;仅root拥有管理其它用户的cron任务的权限;默认管理自己的;

 

                                                                 -l:list,列出任务;

                                                                 -r: remove,移除所有任务;

                                                                 -e:edit,编辑,打开一当前shell会话的默认编辑器来编辑cron任务表;

练习

每周一到周六的凌晨320分,运行cp命令对/etc/目录进行归档另存,存储位置为/backups/etc-YYYY-MM-DD

20 3 * * 1-6/bin/cp  /backups/etc-‘date+%d’

每周日凌晨230分,运行cp命令对/etc/fstab文件进行备份,存储位置为/backup/fstab-YYYY-MM-DD-hh-mm-ss

30 2 * */bin/cp  /etc/fstab-‘date+%Y-%m-%d-%H-%M-%S’