练习 17:任务调度:cronat

    通常我们需要按计划执行程序。例如,让我们想象一下,你需要在每天的半夜备份你的作品。为了在 Linux 中完成它,有一个叫cron的特殊程序。这是一个恶魔,这意味着,当计算机启动后,它就是启动了,并在后台默默等待,在时机到来时为你执行其他程序。cron具有多个配置文件,系统级的,或者用户级的。默认情况下,用户没有crontab,因为没有为它们安排任何东西。这是cron配置文件的位置:

    /etc/crontab - 系统级cron配置文件。
    /var/spool/cron/crontabs/ - 用于存储用户配置文件的目录。

    它的语法足够简单,让我们选取一行:

    1. 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly

    然后拆开:

    1. 17 # 每个小时的第 17 个分钟
    2. * # 月中的每一天
    3. * # 年中的每一月
    4. * # 每个星期
    5. root # 作为 root 用户
    6. cd / # 执行命令 'cd /'
    7. && # 如果 'cd /' 执行成功,那么
    8. run-parts --report /etc/cron.hourly # 执行命令 'run-parts --report /etc/cron.hourly'

    现在我总结cron的格式:

    这是时间格式中,可能的字符的缩略列表:

    • 星号(*) - 字段中的所有值,例如 分钟的*表示每分钟。
    • 斜线(/) - 定义范围的增量。例如,30-40/3意味着在第 30 分钟运行程序,每 3 分钟一次,直到第 40 分钟。
    • 百分比(%) - 在命令字段中,百分比后的所有数据将作为标准输入发送到命令。现在不要纠结这个。
    • 逗号(,) - 指定列表,例如分钟字段的30,40表示第 30 和 40 `分钟。
    • 连字(-) - 范围。例如,分钟字段的30-40意味着每分钟在30到40分钟之间。
    • L - 指定最后一个东西,例如它允许你指定一个月的最后一天。

    现在我会给你一些例子:

    1. # m h dom mon dow user command
    2. # (每月每天每小时)每分钟
    3. * * * * * root /bin/false
    4. # (每月每天)每小时的第 30~40 分钟中的每分钟
    5. # (每月每天)每小时的第 30~40 分钟中的每五分钟
    6. 30-40/5 * * * * root /bin/false
    7. */5 * * * * user command to be executed
    8. # 每月的最后一天的每小时每分钟
    9. * * L * * root /bin/false
    10. # 每月的星期天和星期三的每小时每分钟
    11. * * * * 0,3 root /bin/false
    • crontab -l - 打印出当前的crontab
    • crontab -e - 为当前用户编辑crontab
    • crontab -r - 删除当前用户的crontab
    • crontab /path/to/file - 为当前用户加载crontab,覆盖过程中的现有项。
    • crontab > /path/to/file - 将crontab保存到文件中。

    这就是如何使用cron系统守护进程。但是还有一个可以调度程序执行的选项。它就是at工具。它们之间的区别是,cron为重复运行任务而设计,而且是很多次,并且at为调度一次性的任务而设计。这是相关的命令:

    • at - 在指定的时间执行命令。
    • atq - 列出待处理的任务。
    • atrm - 删除任务。
    • batch - 执行命令,然后系统空转。

    这个信息转储似乎不够,现在我会给你用于at的,时间规范表格,取自 http://content.hccfl.edu/pollock/unix/atdemo.htm。在下面的例子中,假定的当前日期和时间是 2001 年 9 月 18 日星期二上午 10:00。

    现在你将学习如何添加、查看和移除atcrontab任务。

    1. 1: echo 'echo Here I am, sitting in ur at, staring at ur date: $(date) | write user1' | at now + 1 minutes
    2. 2: atq

    等待你的消息出现,按下<ENTER>并输入更多东西:

    现在等待这个消息出现并移除它。

    1. 7: crontab -r
    2. 8: crontab -l
    1. warning: commands will be executed using /bin/sh
    2. job 13 at Thu Jun 28 14:43:00 2012
    3. user1@vm1:~$ atq
    4. 14 Thu Jun 28 14:45:00 2012 a user1
    5. user1@vm1:~$
    6. Message from user1@vm1 on (none) at 14:43 ...
    7. Here I am, sitting in ur at, staring at ur date: Thu Jun 28 14:43:00 MSK 2012
    8. user1@vm1:~$ crontab -l
    9. no crontab for user1
    10. user1@vm1:~$ echo '* * * * * echo Here I am, sitting in ur crontab, staring at ur date: $(date) | write user1' > ~/crontab.tmp
    11. user1@vm1:~$ crontab -l
    12. * * * * * echo Here I am, sitting in ur crontab, staring at ur date: $(date) | write user1
    13. user1@vm1:~$
    14. Message from user1@vm1 on (none) at 14:47 ...
    15. Here I am, sitting in ur crontab, staring at ur date: Thu Jun 28 14:47:01 MSK 2012
    16. EOF
    17. user1@vm1:~$ crontab -r
    18. user1@vm1:~$ crontab -l
    19. no crontab for user1
    20. user1@vm1:~$
    1. at在下一分钟执行命令echo Here I am, sitting in ur at, staring at ur date: $(date) | write user1
    2. 打印at的任务队列。
    3. echo '* * * * * echo Here I am, sitting in ur crontab, staring at ur date: $(date) | write user1写入你的主目录中的crontab.tmp
    4. 打印你当前的crontab,但目前没有东西,所以它只是把这个告诉你。
    5. crontab.tmp的内容加载到你的个人crontab文件。
    6. 打印你当前的crontab。现在有一些东西。
    7. 删除你当前的crontab
    8. 告诉你,你再次没有了crontab
    • 阅读man crontab, man at, 。
    • 让你的系统每 5 分钟告诉你当前时间。
    • 让你的系统在每小时的开始告诉你当前时间。