首先需要知道的是,无论是系统级还是用户级的cron计划都是文本文件,系 统的计划文件存放在/etc/crontab路径下。用户的计划文件放在/var/spool/cron/用户名,不管是哪一种,都可以满足我们定制计划任务的需求。
root用户可以直接对文件进行修改来编写计划任务也可以使用 crontab -e命令,而普通用户只能使用后者。
# 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
用户名 要执行的命令(需要写全命令的路径)(/bin/bash 文件名.sh 把要执行的命令写到文件里)
查看计划任务的执行:tail -f /var/log/cron
crontab例子
45 4 1,10,22 * * /usr/local/etc/rc.d/apache restart #每月1、10、22日的4 : 45重启apache
10 1 * * 6,0 /usr/local/etc/rc.d/apache restart #每周六、周日的1 : 10重启apache
0,30 18-23 * * * /usr/local/etc/rc.d/apache restart #每天18 : 00至23 : 00之间每隔30分钟重启apache
0 23 * * 6 /usr/local/etc/rc.d/apache restart #每星期六的11 : 00 pm重启apache
* 23-7/1 * * * /usr/local/etc/rc.d/apache restart #晚上11点到早上7点之间,每隔一个小时的每分钟重启 apache
0 */1 * * * /usr/local/etc/rc.d/apache restart #每一小时重启apache
0 4 1 jan * /usr/local/etc/rc.d/apache restart #一月一号的4点重启apache
*/30 * * * * /usr/sbin/ntpdate 210.72.145.44 #每半小时同步一下时间
0 */2 * * * www /usr/bin/somecommand >> /dev/null 2>&1 #以用户www的身份每两小时就运行某个程序: