Linux crontab的使用方式,sh脚本的编写,sh脚本自动启动tomcat服务器,sh监控系统运行情况
2014-05-12 23:21
1466 查看
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
/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监控系统运行情况
- linux使用shell脚本管理其他服务器Tomcat启动停止、自动发布
- 服务器环境之6:使用maven自动部署到tomcat及编写sh一键部署脚本
- [置顶] 让Linux系统启动过程自动运行自己编写的脚本
- 让一个脚本自动定时运行,设置、启动 linux定时脚本 (crontab详解)
- crontab详解-如何让脚本自动定时运行,设置、启动 linux定时器
- tomcat运行监控脚本,自动启动
- Linux下使用shell脚本自动监控重启tomcat
- linux下tomcat随系统自动启动,最简单的方式
- shell脚本监控linux系统内存使用情况的方法(不使用nagios监控linux)
- linux系统使用python监控apache服务器进程脚本分享
- Linux系统下怎样利用nc命令来监控检测服务器的端口使用情况
- Linux下使用shell脚本自动监控重启tomcat
- 编写shell脚本监控系统负载、CPU和内存使用情况
- Linux系统下tomcat自动拉取代码打包发布的配置部署和脚本编写(转朋友)
- linux下收集系统硬盘,内存使用情况以定时邮件方式发给root的一个脚本
- linux 使用sh启动多jar包 编写三个脚本
- shell脚本监控linux系统内存使用情况的方法(不使用nagios监控linux)
- 实战Nagios NSCA方式监控Linux系统资源使用情况 -- Nagios配置篇 -- 被监控端
- 实战Nagios NSCA方式监控Linux系统资源使用情况 -- Nagios配置篇 -- Nagios Server端