Linux crontab的使用方式,sh脚本的编写,sh脚本自动启动tomcat服务器,sh监控系统运行情况
1、如果想使用Linux crontab(類似java quartz),需要先啟動crontab.關于crontab的啟動、關閉、重啟、重新載入配置的方式如下:
/sbin/service crond start //啟動服務
/sbin/service crond stop //關閉服務
/sbin/service crond restart //重啟服務
/sbin/service crond reload //重新載入配置
2、crontab的命令介紹:
A:添加crontab的命令是:crontab -e?? 然后再打開的文件中編寫你要寫的內容(使用方式類似vi工具)
方法二是:直接編輯/etc/crontab 文件,即vi /etc/crontab,添加相應的任務
B:列出當前所有的調度任務:crontab -l
C:刪除所有任務調度工作:crontab -r
D:任務調度的cron表達式
* * * * * program
分 時 日 月 周 ?????? 命令
第1列表示分鐘1~59 每分鐘用*或者 */1表示
第2列表示小時1~23(0表示0點)
第3列表示日期1~31
第4列表示月份1~12
第5列標識號星期0~6(0表示星期天)
第6列要運行的命令
當第1列 為 * 時表示每分鐘都要執行 program,第2列為 * 時表示每小時都要執行程式,其余類推
當第1列為 a-b 時表示從第 a 分鐘到第 b 分鐘這段時間內要執行,第2列為 a-b 時表示從第 a 到第 b 小時都要執行,其余類推
當第1列為 */n 時表示每 n 分鐘個時間間隔執行一次,第2列 為 */n 表示每 n 小時個時間間隔執行一次,其余類推
當第1列為 a, b, c,... 時表示第 a, b, c,... 分鐘要執行,第2列 為 a, b, c,... 時表示第 a, b, c...個小時要執行,其余類推
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
----------------------------------------------
案例通過crontab在指定時間重啟tomcat,或者監控web項目的啟動與否來重啟tomcat:
1、編寫?? crontabtongweb.sh(并賦予這個文件可執行的權限:chmod 777 crontabtongweb.sh)
代碼如下:
#/usr/tomcat7/apache-tomcat-7.0.47/bin/shutdown.sh
#sleep 1m
#/usr/tomcat7/apache-tomcat-7.0.47/bin/startup.sh
#echo test >> $(date -d "today" +"%Y%m%d_%H%M%S").log
#echo test >> $(date -d "today" +"%Y%m%d").log
#kill tomcat pid
ps aux|grep tongweb|grep start|awk '{print $2}'|xargs kill -9
#log
#echo 'kill tongweb pid' >> $(date -d "today" +"%Y-%m-%d").log
#sleep 1m
/root/TongWeb5.0/bin/start.sh
echo $(date -d "today" +"%Y-%m-%d %H:%M:%S") tongweb restart >>/root/TongWeb5.0/crontab_log/css_restart.log
保存這些配置,然后給
2、編寫crontabmonitor.sh(并給文件賦予可執行的權限)
#/usr/tomcat7/apache-tomcat-7.0.47/bin/shutdown.sh
#sleep 1m
#/usr/tomcat7/apache-tomcat-7.0.47/bin/startup.sh
#echo test >> $(date -d "today" +"%Y%m%d_%H%M%S").log
#echo test >> $(date -d "today" +"%Y%m%d").log
rm -f index.html
wget -T 10 -t 3 -q http://192.168.58.2/swordcms/
if [ ! -e index.html ]; then
?? #kill tomcat pid
?? ps aux|grep tongweb|grep start|awk '{print $2}'|xargs kill -9
?? #log
?? #echo 'kill tongweb pid' >> $(date -d "today" +"%Y-%m-%d").log
?? #sleep 1m
?? /root/TongWeb5.0/bin/start.sh
?? echo $(date -d "today" +"%Y-%m-%d %H:%M:%S") tongweb error restart >>/root/TongWeb5.0/crontab_log/css_restart.log
#else
#?? echo $(date -d "today" +"%Y-%m-%d %H:%M:%S") tongweb normal >>/root/TongWeb5.0/crontab_log/css_normal.log
fi
保存配置
3、設置crontab,使用crontab -e編寫如下內容:
* */59 * * * /root/TongWeb5.0/bin/crontabtongweb.sh
* */5 * * * /root/TongWeb5.0/bin/crontabmonitor.sh
4、將配置重新載入:
/sbin/service crond reload
/sbin/service crond restart
總結
以上是生活随笔為你收集整理的Linux crontab的使用方式,sh脚本的编写,sh脚本自动启动tomcat服务器,sh监控系统运行情况的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么看汽车的好坏?
- 下一篇: 比亚迪S6远近灯光不亮?按喇叭就亮了这是