crontab命令

来源:互联网 发布:python 网易公开课 编辑:程序博客网 时间:2024/06/07 02:29

一当前PC 环境: 3.8.0-29-generic #42~precise1-Ubuntu SMP Wed Aug 14 16:19:23 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

useful link: (基本上看完这两个link就够了,我的文章也是整理和总结了这几篇文章,以及其他地方我看到的或者自己学习的觉得有用的东西)
http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html
https://help.ubuntu.com/community/CronHowto

二作用:
crontab是用来定期执行程序的命令。
当安装完成操作系统之后,默认便会启动此任务调度命令。
crontab命令每分钟会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作.

三特性:
Linux下的任务调度分为两类,系统任务调度和用户任务调度
1:
系统任务调度:系统周期性所要执行的工作,比如写缓存数据到硬盘、日志清理等。在/etc目录下有一个crontab文件,这个就是系统任务调度的配置文件。

cat /etc/crontabSHELL=/bin/sh  //指定系统要用的是哪个shellPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin    //指定系统执行命令的路径.#m h dom mon dow user  command 17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly 25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )// run-parts命令的意思就是会执行这个路径下所有的脚本. http://manpages.ubuntu.com/manpages/zesty/en/man8/run-parts.8.html

2:
用户任务调度:用户定期要执行的工作,比如用户数据备份、定时邮件提醒等。用户可以使用crontab工具来定制自己的计划任务。
配置文件:
/etc/cron.deny
该文件中所列用户不允许使用crontab命令, list of users that are not allowed to use cron daemon.
/etc/cron.allow
该文件中所列用户允许使用crontab命令, list of users that are allowed to use cron daemon.
(上面两个文件可有可无.如果有需要也可以自己创建,我的ubuntu上面就没有.)

ls  /var/spool/cron/crontabs/ root  user1  user2

所有用户定义的crontab文件都被保存在/var/spool/cron目录中。其文件名与用户名一致。每个用户都有自己的crontab文件. 系统回去检测执行crontab 文件, 不管对应的用户是否已经登录系统.

四状态:

$ /etc/init.d/cron status Rather than invoking init scripts through /etc/init.d, use the service(8) utility, e.g. service cron status Since the script you are attempting to invoke has been converted to an Upstart job, you may also use the status(8) utility, e.g. status cron cron start/running, process 1739 $ service cron status cron start/running, process 1739service cron start //启动服务service cron stop //关闭服务service cron restart //重启服务service cron reload //重新载入配置

五应用:
1: 命令格式
man crontab/ man 5 crontab (其实文中介绍的很多特性以及环境变量解释等在man crontab/man 5 crontab里面也有介绍)
crontab [ -u user ] file
-u user:用来设定某个用户的crontab服务, file是命令文件的名字,表示将file做为crontab的任务列表文件并载入crontab。
crontab [ -u user ] [ -i ] { -e | -l | -r }
-i:在删除用户的crontab文件时给确认提示。
-e:编辑某个用户的crontab文件内容。如果不指定用户,则表示编辑当前用户的crontab文件。
(打开之后的界面就像是vi编辑器, 可以编辑/添加/删除里面的条目)
-l:显示某个用户的crontab文件内容,如果不指定用户,则表示显示当前用户的crontab文件内容。
(显示输出,可将内容导入其他文件用于备份)
-r:从/var/spool/cron目录中删除某个用户的crontab文件,如果不指定用户,则默认删除当前用户的crontab文件。

2:使用实例

# To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones.

下面这个图片可能解释的更直接点:
这里写图片描述

在以上各个字段中,还可以使用以下特殊字符:
星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。
逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”
中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”
正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。
还有一些特殊字符串:

string meaning @reboot Run once, at startup. @yearly Run once a year, “0 0 1 1 *”. @annually (same as @yearly) @monthly Run once a month, “0 0 1 * *”. @weekly Run once a week, “0 0 * * 0”. @daily Run once a day, “0 0 * * *”. @midnight (same as @daily) @hourly Run once an hour, “0 * * * *”.

实例1:每1分钟执行一次command
命令:
* * * * * command/script

实例2:每小时的第3和第15分钟执行
命令:
3,15 * * * * command/script

实例3:在上午8点到11点的第3和第15分钟执行
命令:
3,15 8-11 * * * command/script

实例4:每隔两天的上午8点到11点的第3和第15分钟执行
命令:
3,15 8-11 /2 * command/script

实例5:每个星期一的上午8点到11点的第3和第15分钟执行
命令:
3,15 8-11 * * 1 command/script

实例15:每小时执行/etc/cron.hourly目录内的脚本
命令:
01 * * * * root run-parts /etc/cron.hourly

六常见问题:

问题1: 误删了crontab文件
grep CRON /var/log/syslog 可以查看到历史执行的crontab job相关的命令. 最好的方法还是提前备份.

问题2: https://www.cyberciti.biz/faq/linux-show-what-cron-jobs-are-setup/
http://2clickfix.com/6-reasons-cron-job-not-running/
文章中有有些crontab的常见问题,可以看看.