Linux计划任务(at batch crontab anacron)

简介:
·     未来只做一次                             at          ( 依赖于 atd 服务 )
·     未来周期性做                             cron       ( 依赖于 crond 服务 )
·     cron 补充 ( 开机后执行 )                anacron   ( 依赖于 anacron 服务 )
 
 
  1. at
  2. at 命令被用来在指定时间内调度一次性的任务。 
  3.  
  4. at [-mldv] TIME 
  5. 选项与参数: 
  6. -m  :当at的任务完成后,即使没有输出信息,也以 email 通知给使用者 
  7. -l  :列出目前系统上面的所有该使用者的at任务(同atq) 
  8. -d  :可以取消一个在 at 任务(同atrm) 
  9. -v  :可以使用较明显的时间格式列出 at 任务 
  10. -c  :可以列出后面接的该项任务的内容 
  11.  
  12. at命令的时间格式: 
  13. now + 时间    :时间以 minutes、hours、days、或 weeks 为单位 
  14. HH:MM    :24小时制度,如果时间已过,就会在第二天的这一时间执行 
  15. midnight     :表示00:00 
  16. noon     :表示12:00 
  17. teatime  :表示16:00 
  18.  
  19. [root@rhel6 ~]# at 13:10                    //定义一个at任务在13:10执行 
  20. atdate >> /tmp/at                      //将当前时间输入/tmp/at文件 
  21. at> echo "at command test" >> /tmp/at 
  22. at> uname -r >> /tmp/at 
  23. at> <EOT>                            //另选一行按Ctrl+D退出at命令模式 
  24. job 1 at 2012-12-28 13:10 
  25. [root@rhel6 ~]# at now + 1 minutes          //再定义一个at任务在1分钟后执行 
  26. atdate >> /tmp/at 
  27. at> echo "at command test again" >> /tmp/at 
  28. at> sleep 10 
  29. atdate >> /tmp/at 
  30. at> <EOT> 
  31. job 2 at 2012-12-28 13:10 
  32. [root@rhel6 ~]# date 
  33. Fri Dec 28 13:09:09 CST 2012 
  34. [root@rhel6 ~]# atq                      //显示系统中所有的at任务 
  35. 2       2012-12-28 13:10 a root 
  36. 1       2012-12-28 13:10 a root 
  37. [root@rhel6 ~]# cat /tmp/at 
  38. Fri Dec 28 13:10:00 CST 2012 
  39. Fri Dec 28 13:10:00 CST 2012 
  40. at command test 
  41. at command test again 
  42. 2.6.32-220.el6.x86_64 
  43. Fri Dec 28 13:10:10 CST 2012                //对比两个时间可见at任务是按顺序执行命令的 
  44.  
  45.  
  46. 补充:batch 
  47. batch 命令被用来在系统平均负载达到 0.8% 以下时执行一次性的任务,用法与at一样 
  48. [root@rhel6 ~]# batch midnight              //在00:00之后系统平均负载达到0.8%以下的时候执行 
  49. at> sync 
  50. at> sync 
  51. at> shutdown -h now 
  52. at> <EOT> 
  53. job 3 at 2012-12-28 13:20 
  54. [root@www ~]# atq 
  55. 3       2012-12-28 13:20 b root 
  56. [root@www ~]# atrm 3                        //取消batch任务 
  57.  
  58. /etc/at.allow 和 /etc/at.deny 文件可用来限制对 at 和 batch 命令的使用(root用户不受其控制)。这两个使用控制文件的格式都是每行一个用户(不允许空格),且文件修改后,atd守护进程不需重启。 
  59. 如果at.allow文件存在,那么只有其中列出的用户才被允许使用at或batch命令,且忽略cron.deny文件。如果at.allow文件不存在,那么所有在cron.deny中列出的用户都将禁止使用at和batch. 
 
 
  1. cron: 
  2. crond是Linux下用来周期性地执行某种任务或等待处理某些事件的一个守护进程,且修改任务或控制文件后不用重启。 
  3. 当完成操作系统的安装后,默认会安装crond,并自动启动crond进程。crond进程每分钟会定期检查是否有要执行的任务。 
  4. 其中/etc/cron.allow和/etc/cron.deny文件用来现在cron的使用。这两个文件的格式都是每行一个用户(不允许空格),但root用户不受其限制。 
  5. cron.allow、cron.deny规则与at命令控制文件相同 
  6.  
  7. Linux下的任务调度分为两类:系统任务调度和用户任务调度 
  8. •   用户任务调度:用户定期要执行的工作。通过crontab命令定制。所有用户定义的crontab文件都保存在/var/sopl/cron目录中(文件名与用户名一致) 
  9. •   系统任务调度:系统周期行所要执行的任务。通过/etc/crontab配置 
  10.  
  11. crontab文件格式: 
  12. minute  hour    day month   week    command 
  13. 0-59    0-23    1-31    1-12    0-7  
  14. 另外"*"表示所有可能的值,","表示某个间隔时段,"-"表示某个连续时段,"/n"表示每隔n个单位时间 
  15.  
  16. 0   */3 *   *   *                    //表示每隔3个小时 
  17. 30  3   *   *   6                    //表示每周6的3:30 
  18. 0   0   1,20    *   *                //表示每个月的1号和20号 
  19. 10  5   */5 *   *                    //表示每个月的5、10、15、20、25、30号的5:10 
  20.  
  21. 用户任务调度: 
  22. crontab [-u user] [file] 
  23. crontab [-u user] [-e | -l | -r | -i] 
  24. -u user :用来设定某个用户的crontab任务 
  25. -e      :编辑某个用户的crontab任务(如果是由系统执行的crontab任务可之间编辑/etc/crontab文件) 
  26. -l      :显示某个用户的crontab任务 
  27. -r      :删除某个用户的所有crontab任务(从/var/spool/cron目录中删除该用户的crontab文件) 
  28. -i      :在删除用户的crontab文件时给确认提示 
  29. 每次编辑完某个用户的cron设置后,cron会自动在/var/spool/cron/目录下生成一个与用户同名的文件记录该用户的cron信息 
  30.  
  31. [root@rhel6 ~]# ll /var/spool/cron/ | grep root 
  32. [root@rhel6 ~]# crontab -e                       //创建crontab任务 
  33. * * * * * date >> /tmp/crontab               //要求每分钟向/tmp/crontab输入当前时间的信息 
  34. 注:单纯date的话将看不到任何输出,因为cron把任何输出都mail到root用户了 
  35. [root@rhel6 ~]# crontab -l 
  36. * * * * * date >> /tmp/crontab 
  37. [root@rhel6 ~]# ls /var/spool/cron/ | grep root     //自动在/var/spool/cron/目录中生成一个以root为文件名的文件 
  38. -rw-------. 1 root root 28 Dec 27 17:15 root        //crontab任务于17:15分创建 
  39. [root@rhel6 ~]# cat /tmp/crontab 
  40. Thu Dec 27 17:16:01 CST 2012                     //从17:16分开始每分钟向/tmp/crontab文件输入当前时间的信息 
  41. Thu Dec 27 17:17:01 CST 2012 
  42. Thu Dec 27 17:18:01 CST 2012 
  43. Thu Dec 27 17:19:01 CST 2012 
  44. Thu Dec 27 17:20:01 CST 2012 
  45. [root@rhel6 ~]# crontab -r                   //删除用户的所有crontab任务 
  46. [root@rhel6 ~]# crontab -l 
  47. no crontab for root 
  48. [root@rhel6 ~]# ll /var/spool/cron/ | grep root     //同时从/var/spool/cron目录中删除该用户的crontab文件 
  49.  
  50. 系统任务调度: 
  51. 可通过直接编辑/etc/crontab文件进行计划任务的管理,该文件与"crontab -e" 的内容几乎完全一模一样。 
  52. [root@rhel6 ~]# cat /etc/crontab  
  53. SHELL=/bin/bash                      //执行任务所使用的shell类型 
  54. PATH=/sbin:/bin:/usr/sbin:/usr/bin           //执行任务所使用的PATH 
  55. MAILTO=root                          //若有标准(错误)输出信息,则mail给root 
  56. HOME=/                               //shell所在的家目录 
  57.  
  58. # *  *  *  *  * user-name command to be executed    //以下为计划任务的内容(与crontab -e的格式是一样的) 
  59. # run-parts 
  60. 01 * * * * root run-parts /etc/cron.hourly       //每小时01分以root身份执行/etc/cron.hourly/目录下的脚本 
  61. 02 4 * * * root run-parts /etc/cron.daily 
  62. 22 4 * * 0 root run-parts /etc/cron.weekly 
  63. 42 4 1 * * root run-parts /etc/cron.monthly 
 
 
  1. anacron: 
  2. anacron只是cron的补充而非是完全替代 cron。 
  3. cron 是作为守护进程运行的,而anacron则作为普通进程运行并终止的。anacron 维护了一组应当运行的任务,每个任务都一个相关的运行间隔。anacron 并不能指定何时运行某项任务, 而是以天为单位或者是在启动后立刻进行 anacron 的动作,当系统启动之后anacrod将会去侦测停机期间应该进行但是并没有进行的 crontab 任务,并将该任务运行一遍后,然后 anacron 就会自动停止了。 
  4. 因此 anacron 运行的时间通常有两个,一个是系统启动期间运行,一个是写入 crontab 中的任务。 
  5.  
  6. [root@rhel6 ~]# cat /etc/anacrontab 
  7. SHELL=/bin/sh 
  8. PATH=/sbin:/bin:/usr/sbin:/usr/bin 
  9. MAILTO=root 
  10. # the maximal random delay added to the base delay of the jobs 
  11. RANDOM_DELAY=45 
  12. # the jobs will be started during the following hours only 
  13. START_HOURS_RANGE=3-22 
  14. #天数      延迟时间     任务名称定义       执行的指令 
  15. #period in days  delay in minutes    job-identifier     command 
  16. 1             5            cron.daily         nice run-parts /etc/cron.daily 
  17. #由 /etc/anacrontab 分析到 cron.daily 这项工作名称的天数为 1 天; 
  18. #由 /var/spool/anacron/cron.daily 取出最近一次运行 anacron 的时间戳记; 
  19. #由时间戳与目前的时间比较,若间隔天数为 1 天(以上),将延迟5分钟后执行"nice run-parts /etc/cron.daily"这条命令,且运行完毕后anacron程序结束 
  20. 7            25       cron.weekly        nice run-parts /etc/cron.weekly 
  21. @monthly     45       cron.monthly       nice run-parts /etc/cron.monthly 
 


     本文转自Vnimos51CTO博客,原文链接: http://blog.51cto.com/vnimos/1103353 ,如需转载请自行联系原作者


相关文章
|
3月前
|
存储 Linux Shell
Linux中的计划任务—Crontab调度一次性执行的任务at/batch
Linux中的计划任务—Crontab调度一次性执行的任务at/batch
118 0
|
4天前
|
消息中间件 监控 Linux
Linux进程和计划任务管理
通过这些命令和工具,你可以有效地管理Linux系统中的进程和计划任务,监控系统的运行状态并保持系统的稳定和可靠性。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
98 2
|
10天前
|
Linux
Linux Crontab 查看定时任务启动没
Linux Crontab 查看定时任务启动没
16 0
Linux Crontab 查看定时任务启动没
|
28天前
|
存储 Shell Linux
【Shell 命令集合 系统设置 】Linux定时任务 crontab命令 使用指南
【Shell 命令集合 系统设置 】Linux定时任务 crontab命令 使用指南
35 0
|
2月前
|
Linux Shell 数据库
Linux如何在一个 Crontab 中安排多个 Cron 作业?
Linux如何在一个 Crontab 中安排多个 Cron 作业?
43 1
|
4月前
|
运维 Linux 应用服务中间件
Linux 定时任务crontab实现秒级定时以及@reboot的一些问题
Linux 定时任务crontab实现秒级定时以及@reboot的一些问题
108 0
|
4月前
|
关系型数据库 Linux Shell
Linux|奇怪的知识|一次性任务at命令的使用
Linux|奇怪的知识|一次性任务at命令的使用
35 0
|
4月前
|
Linux
Linux - 一次性计划任务之at命令使用
Linux - 一次性计划任务之at命令使用
74 0
|
11天前
|
Web App开发 Linux 网络安全
工作中常用到的Linux命令
工作中常用到的Linux命令
|
11天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇