crontab 小结

来源:互联网 发布:淘宝弹出淘口令 编辑:程序博客网 时间:2024/06/05 17:32

文章出处:http://blog.csdn.net/shift_wwx


前言:今天碰到一个问题,有个脚本手动能成功执行,可是用crontab却是不能成功,知道为什么吗?


1、简介

crontab文件包含送交cron守护进程的一系列作业和指令。每个用户可以拥有自己的crontab文件;同时,操作系统保存一个针对整个系统的crontab文件,该文件通常存放于/etc或者/etc之下的子目录中,而这个文件只能由系统管理员来修改。
crontab文件的每一行均遵守特定的格式,由空格或tab分隔为数个领域,每个领域可以放置单一或多个数值。


2、配置介绍

主要配置文件是/etc/crontab

# /etc/crontab: system-wide crontab# Unlike any other crontab you don't have to run the `crontab'# command to install the new version when you edit this file# and files in /etc/cron.d. These files also have username fields,# that none of the other crontabs do.SHELL=/bin/shPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# m h dom mon dow user  command17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly25 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 )
SHELL 变量的值告诉系统要使用哪个 shell 环境(在这个例子里是 /bin/sh);PATH 变量定义用来执行命令的路径。

注释的部分# m h dom mon dow user  command

m:minute (0~59)

h:hour (0~23,0代表午夜)

dom:day of month (1~31)

mon:month (1~12)

dow:day of week (0~6,0代表周日)

user:user

command:需要执行的命令

*:星号代表任何值,也可以是具体的值,也可以是范围。

例如,dom可以1-5,代表1、2、3、4、5,每个月的1、2、3、4、5号执行命令。

例如,dom可以是1,3,5,7代表每个月的1,3,5,7号的时候执行命令。

例如,dom可以用*/3,代表每三天执行一次命令

当然也可以组合使用,例如dom可以是1-5,10-13代表是每个月的1-5号和10-13号执行命令。


3、由于Cron 是Linux的内置服务,可以用以下的方法启动、关闭这个服务:
/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置


4、cron.d/       cron.daily/   cron.hourly/  cron.monthly/ cron.weekly/

cron.daily是每天执行一次的job,cron.weekly是每个星期执行一次的job.
cron.monthly是每月执行一次的job,cron.hourly是每个小时执行一次的job.

cron.d是系统自动定期需要做的任务,但是又不是按小时,按天,按星期,按月来执行的,那么就放在这个目录下面。
如果是按小时,按天,按星期,按月的来执行的话,则可以放到前面相应的目录下面去

例如:

wwx@shift:/etc/cron.daily$ ls -ltotal 68-rwxr-xr-x 1 root root   311 Jun 20  2010 0anacron-rwxr-xr-x 1 root root   219 Apr 10  2012 apport-rwxr-xr-x 1 root root 15399 Apr 20  2012 apt-rwxr-xr-x 1 root root   314 Mar 31  2012 aptitude-rwxr-xr-x 1 root root   502 Mar 31  2012 bsdmainutils-rwxr-xr-x 1 root root   256 Apr 13  2012 dpkg-rwxr-xr-x 1 root root   372 Oct  5  2011 logrotate-rwxr-xr-x 1 root root  1365 Apr  1  2012 man-db-rwxr-xr-x 1 root root   606 Aug 17  2011 mlocate-rwxr-xr-x 1 root root   249 Apr  9  2012 passwd-rwxr-xr-x 1 root root  2417 Jul  2  2011 popularity-contest-rwxr-xr-x 1 root root   383 Apr 13  2012 samba-rwxr-xr-x 1 root root  2947 Apr  2  2012 standard-rwxr-xr-x 1 root root   214 Apr 20  2012 update-notifier-common

5、crontab的语法

usage:crontab [-u user] filecrontab [ -u user ] [ -i ] { -e | -l | -r }(default operation is replace, per 1003.2)-e(edit user's crontab)-l(list user's crontab)-r(delete user's crontab)-i(prompt before deleting user's crontab)

-l:列出crontab文件
-e:编辑crontab文件
-r:删除crontab文件
-i:在-r的时候,提醒用户
-u:指定用户

任何不确定因素可以参考man:

NAME       crontab - maintain crontab files for individual users (Vixie Cron)SYNOPSIS       crontab [ -u user ] file       crontab [ -u user ] [ -i ] { -e | -l | -r }DESCRIPTION       crontab  is the program used to install, deinstall or list the tables used to drive the cron(8) daemon in Vixie Cron.  Each user can       have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly.       If the /etc/cron.allow file exists, then you must be listed (one user per line) therein in order to be allowed to use this  command.       If the /etc/cron.allow file does not exist but the /etc/cron.deny file does exist, then you must not be listed in the /etc/cron.deny       file in order to use this command.       If neither of these files exists, then depending on site-dependent configuration parameters, only the super user will be allowed  to       use this command, or all users will be able to use this command.       If  both  files exist then /etc/cron.allow takes precedence. Which means that /etc/cron.deny is not considered and your user must be       listed in /etc/cron.allow in order to be able to use the crontab.       Regardless of the existance of any of these files, the root administrative user is always allowed to setup a crontab.  For  standard       Debian systems, all users may use this command.       If  the -u option is given, it specifies the name of the user whose crontab is to be used (when listing) or modified (when editing).       If this option is not given, crontab examines "your" crontab, i.e., the crontab of the person  executing  the  command.   Note  that       su(8) can confuse crontab and that if you are running inside of su(8) you should always use the -u option for safety's sake.       The  first form of this command is used to install a new crontab from some named file or standard input if the pseudo-filename ``-''       is given.       The -l option causes the current crontab to be displayed on standard output. See the note under DEBIAN SPECIFIC below.       The -r option causes the current crontab to be removed.       The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment  variables.   After       you  exit from the editor, the modified crontab will be installed automatically. If neither of the environment variables is defined,       then the default editor /usr/bin/editor is used.       The -i option modifies the -r option to prompt the user for a 'y/Y' response before actually removing the crontab.DEBIAN SPECIFIC       The "out-of-the-box" behaviour for crontab -l is to display the three line "DO NOT EDIT THIS FILE" header  that  is  placed  at  the       beginning of the crontab when it is installed. The problem is that it makes the sequence       crontab -l | crontab -       non-idempotent  --  you keep adding copies of the header. This causes pain to scripts that use sed to edit a crontab. Therefore, the       default behaviour of the -l option has been changed to not output such header. You may obtain the original behaviour by setting  the       environment variable CRONTAB_NOHEADER to 'N', which will cause the crontab -l command to emit the extraneous header.SEE ALSO       crontab(5), cron(8)FILES       /etc/cron.allow       /etc/cron.deny       /var/spool/cron/crontabs       There  is  one  file  for  each user's crontab under the /var/spool/cron/crontabs directory. Users are not allowed to edit the files       under that directory directly to ensure that only users allowed by the system to run periodic tasks can add them, and only syntacti‐       cally  correct crontabs will be written there.  This is enforced by having the directory writable only by the crontab group and con‐       figuring crontab command with the setgid bid set for that specific group.

通过man,可以看出,/etc/cron.allow 和 /etc/cron.deny很关键的两个文件。

如果/etc/cron.allow文件存在的话,只有列出来的用户来能使用cron的命令,这个时候/etc/cron.deny是被忽略的。

如果/etc/cron.allow文件不存在,但是/etc/cron.deny存在的话,列出的用户名不能执行cron的命令,即想要执行命令,不能被列出来。

如果两个文件都不存在的话,就需要依据一定的配置参数(没看懂,谁给解释一下),只有root用户才能执行命令,或者是所有用户都可以执行命令。

我大概理解为,如果两个文件都没有的话,只会理解为root用户在执行crontab的命令,这就可以解释,为什么手动执行脚本可以,但是crontab执行就不行了。那中间肯定存在差异,例如是普通用户可能另外多加了一些环境变量,可是root是没有的,那样缺少了环境,当然不能成功执行脚本了!


参考文献:

http://baike.baidu.com/link?url=h3VkVWI6XtafzWGi9q7jPrsbRB7b-EykBwrDapoaW09hpIyXH4Lp5RSj-R-uthvrsJe_4OYEF21dwe9quR1zsK


0 0