debian 定时任务 Crontab 使用方法
Debian
/etc/init.d/cron start #启动
/etc/init.d/cron restart #重启命令
Linux
/etc/rc.d/init.d/crond start #启动
/etc/rc.d/init.d/crond restart #重启命令
Crontab 文件格式
用户所建立的 crontab 文件中,每一行都代表一项任务,每行的每个字段代表一项设置,它的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段,格式如下:
每一列的内容分别是:
分钟,可以是从 0 到 59 之间的任何整数。
小时,可以是从 0 到 23 之间的任何整数(0 表示 0 点)。
日期,可以是从 1 到 31 之间的任何整数。
月份,可以是从 1 到 12 之间的任何整数。
星期几,可以是从 0 到 7 之间的任何整数,这里的 0 或 7 代表星期日。
要执行的命令,可以是系统命令,也可以是自己编写的脚本文件。
在以上各个字段中,还可以使用以下特殊字符:
星号(*):代表所有可能的值,例如 month 字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。
逗号(,):可以用逗号隔开的值指定一个列表范围,例如 1,2,5,7,8,9。
中杠(-):可以用整数之间的中杠表示一个整数范围,例如 2-6 表示 2,3,4,5,6。
正斜线(/):可以用正斜线指定时间的间隔频率,例如 0-23/2 表示每两小时执行一次。
同时正斜线可以和星号一起使用,例如 */10,如果用在 minute 字段,表示每十分钟执行一次。又如,每分钟可以用 * 或者 */1 表示。
每五分钟执行 */5 * * * *
每小时执行 0 * * * *
每天执行 0 0 * * *
每周执行 0 0 * * 0
每月执行 0 0 1 * *
每年执行 0 0 1 1 *
每隔5分钟执行某个任务
1、第一种*/,有的系统不太兼容
*/5**** /usr/bin/test.sh
2、第二种,繁琐,所有系统支持
0,5,10,15,20 **** /usr/bin/test.sh
注意:当程序在你所指定的时间执行后,系统会寄一封邮件给你,显示该程序执行的内容,若你不希望收到这样的信,请在每一行空一格之后加上 > /dev/null 2>&1,否则会产生不必要的浪费
设定和管理计划任务
编辑Crontab,来设定计划任务。使用下面的命令,会在vi 里打开crontab的内容以供编辑:
crontab -e
如果你只想看看,不需要编辑,可以使用以下的命令
crontab -l
要删除crontab的内容,就是删除所有的计划任务,可以这样:
crontab -r
/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/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
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 )
#
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭