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/servi

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

目录
相关文章
|
24天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
106 1
|
2月前
|
监控 关系型数据库 Linux
|
2天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
39 2
|
6天前
|
网络协议 安全 Linux
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
|
6天前
|
应用服务中间件 Linux 网络安全
Tomcat的安装(Linux版)
Tomcat的安装(Linux版)
17 0
|
13天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
143 0
|
13天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
139 0
|
17天前
|
弹性计算 Shell Linux
ecs服务器shell常用脚本练习(六)
【4月更文挑战第4天】shell代码训练(六)
110 0
|
22天前
|
弹性计算 Shell 应用服务中间件
ecs服务器shell常用脚本练习(四)
【4月更文挑战第4天】shell代码训练(四)
96 0
|
24天前
|
Ubuntu Linux 虚拟化
【Linux】ubuntu安装samba服务器
【Linux】ubuntu安装samba服务器