上海虹口建设局官方网站,成都微商城开发公司,做网站图片和文字字体侵权,用PS怎么做网站界面1、脚本简介 tomcat看门狗#xff0c;在tomcat进程异常退出时会自动拉起tomcat进程并记录tomcat运行的日志。 1 函数说明#xff1a;
2 log_info#xff1a;打印日志的函数#xff0c;入参为需要在日志中打印的msg
3 start_tom#xff1a;启动tomcat的函数…1、脚本简介 tomcat看门狗在tomcat进程异常退出时会自动拉起tomcat进程并记录tomcat运行的日志。 1 函数说明
2 log_info打印日志的函数入参为需要在日志中打印的msg
3 start_tom启动tomcat的函数
4 check_tom_run:每隔30s检测tomcat进程是否存在
5 log_backup:备份tomcat监控日志 check_tom_run每隔30s检测tomcat进程是否存在log_info用于记录tomcat运行日志和操作日志。tomcat进程不存在时执行start_tom去启动。当日志文件大小大于指定大小时则备份监控日志。2、前提条件 a、需要一台Linux主机 b、主机上已部署tomcat 访问地址http://localhost:8080/ 如果出现以下界面则说明tomcat已成功启动。 3、脚本代码 脚本代码如下 1 #!/bin/bash2 #以下为多行注释3 : !4 作用tomcat watch dog用于在tomcat进程退出后自动拉起tomcat5 函数说明6 log_info打印日志的函数入参为需要在日志中打印的msg7 start_tom启动tomcat的函数8 check_tom_run:每隔10s检测tomcat进程是否存在9 log_backup:备份tomcat监控日志10 !11 12 curr_pathpwd13 #tomcat的安装路径14 tom_path/home/stephen/InstallPath/apache-tomcat-8.5.3915 16 17 #定义打印日志的函数18 function log_info(){19 local curr_timedate %Y-%m-%d %H:%M:%S20 log_file${curr_path}/tom_running.log21 #判断日志文件是否存在22 if [ -e ${log_file} ]23 then24 #检测文件是否可写25 if [ -w ${log_file} ]26 then27 #若文件无写权限则使用chmod命令赋予权限28 chmod 770 ${log_file}29 fi30 else31 #若日志文件不存在则创建32 touch ${log_file}33 fi34 #写日志35 local info$136 echo ${curr_time} whoami [Info] ${info}${log_file}37 }38 39 function start_tom(){40 log_info Begin to start tomcat.41 cd ${tom_path}/bin42 log_info cd ${tom_path}/bin43 sh startup.sh44 log_info sh startup.sh45 #使用ps命令awk获取tomcat的PID46 tom_pidps -aux|grep apache-tomcat|grep -v grep|awk {print $2}47 if [ -z ${tom_pid} ]48 then49 sh startup.sh50 fi51 log_info End to start tomcat.52 }53 54 #如果tomcat_pid为零则说明tomcat进程不存在需要去重启55 function check_tom_run()56 {57 tom_pidps -aux|grep apache-tomcat|grep -v grep|awk {print $2}58 echo ${tom_pid}59 if [ -z ${tom_pid} ]60 then61 echo tomcat process is not running.62 #调用函数start_tom63 start_tom64 log_info tomcat process is not running.65 #打印日志66 else67 echo tomcat process is running68 #打印日志69 log_info tomcat process is running70 fi71 }72 #备份日志73 function log_backup(){74 cd ${curr_path}75 log_nametom_running.log76 #获取当前日志的大小77 log_sizels -all|grep -v ${log_name}.|grep ${log_name}|awk {print $5}78 echo ${log_size}79 #当日志大于150MB时进行备份并清空旧的日志80 expect_sizeexpr 150 \* 1024 \* 102481 echo ${expect_size}82 if [ ${log_size} -gt ${expect_size} ]83 then84 log_info Begin to backup log.85 local ctdate %Y-%m-%d-%H-%M-%S86 cp ${log_name} ${log_name}.${ct}87 log_info cp ${log_name} ${log_name}.${ct}88 #使用gzip命令压缩日志89 gzip -q ${log_name}.${ct} ${log_name}.${ct}.gz90 #清空旧日志91 cat /dev/null ${log_name}92 log_info cat /dev/null ${log_name}93 fi94 }95 96 #隔30s循环执行check_tom_1run97 while [ 1 ]98 do99 check_tom_run
100 log_backup
101 #休眠30s
102 sleep 30
103 done View Code 4、运行结果 4.1、运行脚本命令如下 1 chmod x /tomcatWatchDog.sh #表示脚本后台运行2 ./tomcatWatchDog.sh 4.2、新打开一个窗口杀掉tomcat进程 #获取tomcat的PID
ps -ef|grep tomcat
#kill进程PID为tomcat进程ID
kill -9 PID 4.3、查看tom_running文件从日志来看tomcat进程已自动拉起。 1 2019-04-04 15:23:19 stephen [Info] tomcat process is running2 2019-04-04 15:23:20 stephen [Info] tomcat process is running3 2019-04-04 15:23:34 stephen [Info] tomcat process is running4 2019-04-04 15:24:04 stephen [Info] tomcat process is running5 2019-04-04 15:24:34 stephen [Info] Begin to start tomcat.6 2019-04-04 15:24:34 stephen [Info] cd /home/stephen/InstallPath/apache-tomcat-8.5.39/bin7 2019-04-04 15:24:34 stephen [Info] sh startup.sh8 2019-04-04 15:24:34 stephen [Info] End to start tomcat.9 2019-04-04 15:24:34 stephen [Info] tomcat process is not running.
10 2019-04-04 15:25:04 stephen [Info] tomcat process is running
11 2019-04-04 15:25:34 stephen [Info] tomcat process is running 4.4、当日志文件大小大于指定大小时会备份日志文件。 1 -rwxr-xr-x 1 stephen stephen 2679 4月 4 14:57 tomcatWatchDog.sh*
2 -rwxrwx--- 1 stephen stephen 573893 4月 4 15:28 tom_running.log*
3 -rwxr-x--- 1 stephen stephen 7597 4月 4 12:43 tom_running.log.2019-04-04-12-43-53.g 转载于:https://www.cnblogs.com/webDepOfQWS/p/10655022.html