1.配置好应用程序
如:resin,由于 resin 自动重启后不会主动加载Java环境变量,
所以需要编辑resin/bin目录下的 httpd.sh, 配置其独立运行环境。
# vi httpd.sh (注:首行注入以下内容)
export JAVA_HOME=……
export CLASSPATH=……
export PATH=……
2.然后, 编辑 crontab, 配置定时器。
# crontab -e (注入如下内容)
0 1 * * * /usr/local/resin/bin/httpd.sh restart
(每天凌晨1点自动重启)
30 02 * * * reboot
每天2:30自动重启动系统
(注意, 其中配置的时间以 # clock –show 显示时间作为参照物)
3. 保存后,检查配置内容
# crontab -l
如:
[root@cnux31 ~]# crontab -l
02 03 * * * reboot
[root@cnux31 ~]#
4. 重启 /etc/init.d/ 目录下的 crond, 使配置生效
# crond stop
# crond start
或
# crond restart
5. 查看 cron日志
# ls /var/log/cron* (浏览日志文件)
# cat /var/log/cron (查看详细内容)
参考:
Crontab文件的每一行由六个域(minutes、hours、day of month、month、day of week、 command)组 成,域之间用空格或Tab分开,其中:
minutes: 分钟域,值的范围是0到59
hours: 小时域,值的范围是0到23
day of month: 日期,值的范围是1到31
month: 月份,值的范围是1到12
day of week: 星期,值的范围是0到6,星期日值为0
command: 所要运行的命令
crontab文件的一些例子:
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重启lighttpd 。
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重启lighttpd 。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每周六、周日的1 : 10重启lighttpd 。
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启lighttpd 。
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重启lighttpd 。
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
每一小时重启lighttpd
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11点到早上7点之间,每隔一小时重启lighttpd
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4号与每周一到周三的11点重启lighttpd
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一号的4点重启lighttpd