Crontab

Intro:

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 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. For standard Debian systems, all users may use this command.

Usage:

crontab [ -u user ] file
crontab [ -u user ] { -l | -r [ -i ] | -e }

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.

crontab的命令格式如下:

minute hour day-of-month month-of-year day-of-week commands

即:“分 时 日 月 周 命令 ”,共计6段,前5段都是时间,最后一段才是定期执行的命令,每个时间之间使用空格或者制表符分隔。
前5段即日期和时间的取值范围 :
分钟[00-59]    小时[00-23]    日[01-31]    月份[01-12]    周[0-6](0表示周日)

示例:

#每天8点30分输出一个"Hello World!"
30 08 * * * (echo "Hello World!" > /tmp/hw.txt)

对时间段的控制除了”*”符号外,还有”-”、”/”、”,”。

  • “-”表示时间范围,如[1-30]表示从1到30;
  • “/”表示时间间隔,如 /5 表示每5个时间单位;
  • “,”表示离散的时间点,如 1,3,5,7 等;

示例:

#表示每晚11点开始,每5分钟尝试一次关机
[0-59]/5 23 * * * (/sbin/shutdown -h now)

#要在每个星期一、星期三和星期五的  6:30 a.m. 运行 imacmd 命令
30 6 * * 1,3,5 /usr/bin/imacmd

#在 12 月内, 每天的早上 6 点到 12 点中,每隔3个小时执行一次 /usr/bin/backup :
0 6-12/3 * 12 * /usr/bin/backup

#晚上11点到早上8点之间每两个小时,以及早上8点运行imacmd2命令
0 23-7/2,8 * * * imacmd2
This entry was posted in Unix/Linux. Bookmark the permalink.

4 Responses to Crontab

  1. jianpx says:

    我觉得运行crontab脚本每次都要写绝对路径很麻烦,所以上次查手册后发现在crontab文件开头设置好SHELL 和 PATH两个环境变量后就ok了,很方便。^_^

  2. dhf says:

    我觉得写绝对路径没啥不好啊,不容易搞错

  3. dhf says:

    话说勇爷您就不能将博客内容默认全文输出?您这样对rss user很不友好啊

  4. Stephen says:

    我已经调成将全文输出了 现在格式好看多了 你那里还是一坨文字吗?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>