crontab类似于windows下面的计划任务,是用来定时执行程序的,我也一直习惯于把一些需要经常关心的脚本放入crontab中去执行,并监控起来,比如磁盘空间是否快满了,机器是否reboot过等等,crontab的用法很简单,有几个常用的命令,如下:
crontab -l 显示当前crontab中的任务
crontab -e 编辑当前crontab中的任务
crontab file,重新导入新的任务
我一般的做法就是首先把当前的任务导出到一个文件中,然后vi修改之后,在导入,比如:
crontab -l > myfile
#edit my file here, add my own task
crontab myfile
注意,这里crontab的操作都需要是root用户来执行。
多谢Ningoo地提醒,这里不一定是root用户去执行
crontab文件的格式大概是:
30 * * * * xxxx.script
其中每个星号的意义如下:
minute (0-59),
hour (0-23),
day of the month (1-31),
month of the year (1-12),
day of the week (0-6 with 0=Sunday).
今天发现居然crontab还可以这样写:
@hourly /usr/local/www/awstats/cgi-bin/awstats.sh
使用 @hourly 對應的是 0 * * * *, 還有下述可以使用:
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 * * * *”.
特別是看到 @reboot, 所以寫 rc.local 的東西, 也可以使用 @reboot 寫在 crontab 裡面?
受教了,以后有多了一种选择。
不错,学了一招^_^
注意,这里crontab的操作都需要是root用户来执行。
这个不用一定是root的吧
第一次见,学习一下