Crontab 备忘

cron 是处理定时任务的程序,来源是希腊语里面意思是“时间”的某个词,能够查看它是否是在运行 ps -ef | grep cron | grep -v grep,crontab 里面内容能执行的前提是 cron 必须在运行。
crontab 是 Cron Table 的意思。每一个用户都有本身的,能够在 /var/spool/cron 找到这些文件。可是不要直接编辑它。而是经过命令,分别是列出内容、编辑、删除:php

$ crontab -l
$ crontab -e
$ crontab -r

想备份的话直接把 crontab -l 的内容保存到文件就行了。html

另外系统级别还有这样的目录 /etc/cron.{hourly,daily,weekly,monthly},里面的内容会按照对应的周期执行。linux

Crontab 里面每一项的设置的规则以下:dom

# 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)
# |  |  |  |  |
# *  *  *  *  *   command to be executed

能够写一个 Hello World 来测试,会每分钟把时间打在 ~/date_log 里面:测试

* * * * * date >> date_log

执行 Crontab 里面的命令时产生的 stdout 会经过邮件发送给当前用户。有邮件以后命令行中会提示 You have new mailWhat is the "You have new mail" message in Linux/UNIX? - Super Userui

再多设置几个来作测试吧!url

# m h  dom mon dow   command
* * * * * date >> ~/cron_test/every_minute
10 * * * * date >> ~/cron_test/every_hour_10
44 4 * * * date >> ~/cron_test/every_day_4_44
0 4 1 * * date >> ~/cron_test/every_month_1_4_00

参考:操作系统

相关文章
相关标签/搜索