How to enable crontab on Mac OS/X
http://farm4.static.flickr.com/3038/2991804263_51094429a0.jpg?v=01. Create a new crontab file for your user, Type the following into the terminal:#crontab -e
2. Choose when you would like the job to run. Every jod is a line in your crontab file. The first 5 arguments specify the time to run the job, and the 6th argument is the command to run.
Argument 1: Minute (0 - 59)
Argument 2: Hour (0 - 23)
Argument 3: Day of Month (1 - 31)
Argument 4: Month (1-12)
Argument 5: Day of Week (0 - 6) Sunday = 0
Argument 6: Command
e.g:
0,20,40 * * * * cat /var/log/system.log >> /home/user/logfile.txt 2>&1
will get the log file on system.log at the 0th, 20th, 40th of per hour, and redirect the content to logfile under /home/user.
30 12 * * * touch testfile
will touch a file which name is testfile at 12:30 everyday
Save the file, and crontab should report:
crontab: installing new crontab
Notes: On Mac OS, don't restart the cron service after edit crontab of user
3. To see a list of your crontabs, run this command:
# crontab -l
4. To remove crontabs, run this command:
# crontab -r
For more detailed-chinese information about Cron, please refer to Cron用法说明 and Cron Help Guide
The Details information, please refer to Cron用法说明
cron来源于希腊单词chronos(意为“时间”),是linux系统下一个自动执行指定任务的程序。例如,你想在每晚睡觉期间创建某些文件或文件夹的备份,就可以用cron来自动执行。
服务的启动和停止
cron服务是linux的内置服务,但它不会开机自动启动。可以用以下命令启动和停止服务:/sbin/service crond start
/sbin/service crond stop
/sbin/service crond restart
/sbin/service crond reload
以上1-4行分别为启动、停止、重启服务和重新加载配置。
要把cron设为在开机的时候自动启动,在 /etc/rc.d/rc.local 脚本中加入 /sbin/service crond start 即可。
查看、编辑和删除
cron把命令行保存在crontab(cron table)文件里,这个文件通常在 /etc 目录下。每个系统用户都可以有自己的crontab(在 /var/spool/cron/ 下)。要查看当前用户的crontab,输入 crontab -l;要编辑crontab,输入 crontab -e;要删除crontab,输入 crontab -r。如当前是root身份,要查看/编辑/删除/某用户的crontab,只需在相应的命令后加上 -u USERNAME(如 crontab -e -u USERNAME)即可。crontab文件的默认编辑器是vi,可以输入 export VISUAL='editor' 更改默认编辑器。
cron服务每分钟不仅要读一次 /var/spool/cron 目录内的所有文件,还需要读一次 /etc/crontab 文件。配置这个文件也能让cron执行任务。使用crontab命令是对用户级任务的配置,而编辑 /etc/crontab 文件是对系统级任务的配置。
语法说明
以下是两个cron语句的例子(在 /etc/crontab 文件里)。前者用来晚间备份 /etc 目录,后者运行Analog程序处理服务器的统计信息。12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1
52 5 * * * root /usr/local/src/analog-5.32-lh/analog >> /dev/null 2>&1
以下是cron语句中的字段与字段说明:
字段
说明
1
分钟(0-59)
2
小时(2-24)
3
日期(1-31)
4
月份(1-12;或英文缩写Jan、Feb等)
5
周几(0-6,0为周日;或单词缩写Sun、Mon等)
6
用户名(执行命令时以此用户的身份)
7
要执行的命令(路径)
现在来看第一行:12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1
这条语句将在每天的凌晨3点12分(03:12)运行 tar czf /usr/local/backups/daily/etc.tar.gz /etc 命令。>> /dev/null 2>&1 表示把所有标准输出发送到 /dev/null(linux的回收站),把标准错误输出(2)发送到和标准输出(1)同样的地方(即 /dev/null)。运行这行命令将不会产生任何输出。
这条语句可以变得稍微复杂一点:30 15 13 6 1 * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1
它将在6月13日周一的15:30运行 tar czf /usr/local/backups/daily/etc.tar.gz /etc 命令。
以下语句可以达到同样的效果:30 15 13 Jun Mon * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1
如果你想以用户joey的身份每小时的第15分钟运行某个程序,可以使用:15 * * * * joey /usr/bin/somecommand >> /dev/null 2>&1
其中的星号(*)是通配符,表示cron将忽略这个字段。
如果你想每两小时就运行某个程序,可以在小时字段里使用 */2。它将会在2点,4点,6点……22点,24点运行。具体语句如下:0 */2 * * * joey /usr/bin/somecommand >> /dev/null 2>&1
cron语句中还可以使用逗号(,)来指定多个时间。例如你想在每小时的15分和30分运行某个程序,可以在分钟字段使用 15,30:15,30 * * * * joey /usr/bin/somecommand >> /dev/null 2>&1
如果你想在每月的第一周(即1号到7号)每天的指定时间运行某个程序,可以在日期字段使用 1-7:15,30 */2 1-7 * * joey /usr/bin/somecommand >> /dev/null 2>&1
这条语句将在每月的第1-7日每两小时的15分和30分(02:15,02:30……22: 15,22:30等)运行 /usr/bin/somecommand 命令。
如果你想在每天的16:18执行一个脚本集合,可以把所有要执行的脚本放到一个目录中(如 /home/username/cron),可以使用:18 16 * * * root run-parts /home/username/cron >> /dev/null 2>&1
如果你想保存某个程序的输出结果, 可以把 >> /dev/null 2>&1 替换为 >> /home/user/somecommand.log 2>&1 。
总结
[*]查看当前用户的cron配置,使用 crontab -l
[*]编辑当前用户的cron配置,使用 crontab -e
[*]删除当前用户的cron配置,使用 crontab -r
[*]以root身份查看/编辑/删除某用户的cron配置,在命令后加上 -u USERNAME
[*]配置系统级的任务,编辑 /etc/crontab 文件
页:
[1]