linux environmental processes POST script

Linux shell script under state monitoring and automatic start Tomcat

Recently the company needs to monitor under Linux tomcat service, once tomcat abnormal or service downtime, restart tomcat ensure the normal operation of the service, because under Linux Shell scripts can achieve this effect, it does not consider the use of Java code to write, here is under Linux shell script to monitor the status of Tomcat and implementation steps that starts automatically.

1 preparation of Shell scripts monitor.sh: 
This script code reference to Zhang Xiaofan vip 's blog https://blog.csdn.net/zzq900503/article/details/50723191

#!/bin/sh

# Func: automatic monitoring tomcat script and execute reboot
# author: EagleHao
# DATE: 2018-04-08
# the DEFINE

# Get tomcat process ID (which [grep -w 'tomcat'] code tomcat need to replace your tomcat folder name)
TomcatID = $ (PS -ef | grep tomcat | grep -w 'tomcat' | grep -v 'grep' | awk '{print $ 2}')

# Tomcat startup program (here note that the actual tomcat installation path)
StartTomcat = / usr / local / tomcat / bin / startup.sh
TomcatCache = / usr / local / tomcat / Work

#Define to monitor the page address
WebUrl = http: // localhost: 8080

# Log output
GetPageInfo = / tmp / TomcatMonitor.Info
TomcatMonitorLog = / tmp / TomcatMonitor.log

Monitor ()
{
echo "[info] ... begin monitoring Tomcat [$ (DATE + '% F.% H:% M:% S')]"
IF [$ TomcatID]; where # the then determines whether there is the Tomcat process
echo "[info] tomcat current process ID is: $ TomcatID, continue to detect the page ..."
# detect whether successful start (successful page will return to the state "200")
TomcatServiceCode = $ (curl -s -m 10 -o $ GetPageInfo 10 $ WebUrl-timeout --connect% -w} {HTTP_CODE)
IF [$ 200 is TomcatServiceCode -eq]; the then
echo "[info] page return code $ TomcatServiceCode, tomcat starts successfully, the normal test page"
the else
echo "[error ] tomcat page error, please note ... status code $ TomcatServiceCode, error log is output to the GetPageInfo $ "
echo" [error] error page views, began to restart tomcat "
the kill -9 $ TomcatID # kill the original tomcat process
sleep 3
rm -rf $ TomcatCache # clean up tomcat caching
$ StartTomcat
fi
the else
echo "[error] tomcat process does not exist! tomcat start automatically restart ..."
echo "[info] $ StartTomcat, please wait ..."
RM -rf $ TomcatCache
$ StartTomcat
fi
echo "------- ------------------- "
}
Monitor >> $ TomcatMonitorLog

 

#######

When writing the script encountered a slight problem, the preparation process carelessness, resulting in if the [] less two spaces, as follows:
Malformed: if [$ TomcatID]
correct format: if [$ TomcatID]
if less these two spaces are being given the script execution.

After you finish writing your script, execute the following script:

./monitor.sh
1
If the newspaper permission errors, you need to authorize the script

777 The monitor.sh chmod
1
script is executed successfully, you can go to the / tmp directory below to view the log file TomcatMonitor.log, there is no problem if the test script, the script we started to set up a timed task.
First check whether the system is installed crontab

If not, then install

Installation crontab:

yum install crontabs

Service Instructions:

/ Sbin / service crond start [link here to write content] (https://blog.csdn.net/zzq900503) // start the service

/ Sbin / service crond stop // shut down service

/ Sbin / service crond restart // restart the service

/ Sbin / service crond reload // reload the configuration

/ Sbin / service crond status // start the service

 

After the installation is complete, start crontab service, if the following tips (Centos7 above), then follow the prompts to start the service


crontab service starts after no problem, run crontab -e write a timed task * * * * * /usr/local/monitor.sh executed once per minute monitor.sh script.

Crontab and then restart the service, then go to / tmp below to view the log file TomcatMonitor.log, test results.

Next crontab to boot chkconfig --level 35 crond on, if the following tips (Centos7 above), then follow the prompts to set up


At this point the whole monitoring service tomcat script has been completed, the following is a code analysis:

ps -e // show all processes

ps -f // Press Threaded

grep tomcat // fetch only rows with the tomcat

grep -w 'apache-tomcat-5.5.23' // - w option to search for a word, and to avoid the search word in the string section. Search for the line containing the apache-tomcat-5.5.23

grep -v 'grep' // remove the row containing the grep

awk '{print $ 2}' // dispensing a second column with empty Geqie

ps -ef | grep tomcat | grep -w 'apache-tomcat-5.5.23' | grep -v 'grep' | awk '{print $ 2}' // get tomcat process id

But the mere presence of tomcat process is not enough, you need to visit the site next page to see if normal access to the normal state 200 by curl

curl -s -o $ GetPageInfo -m 10 --connect -timeout 10 $ WebUrl -w% {http_code} // save the page and page content access status code returned
-s --silent // silent mode, and no error is progress
-o $ GetPageInfo // save the file to a local named GetPageInfo $
-m // retrieves pages maximum time (maximum allowable data transmission time)
-m 10 // indicate if unable to complete the acquisition within 10 seconds the page Source operation is abandoned
--connect-timeout // connection timeout
--connect-timeout 10 // indicate if not connected within 10 seconds, then give up
page path variable $ WebUrl // what we want to access
-w // the curl -w parameters we can customize the output of curl,% {http_code} http status code representatives

If the status code is 200 tomcat normal, or kill kill tomcat process, access tomcat startup script tomcat start start.sh

Crontab is then parsed

crontab -e // write a timed task
crontab -l // list the current user cron job
crontab -r // delete the current user cron job

crontab file format:

* * * * * command

minute hour day month week command

Timeshare day week month command
minute: the minutes, and can be any integer from 0-59.

hour: the hour, may be any integer from 0 to 23.

day: the date, can be any integer from 1 to 31.

month: the month, can be any integer from 1-12.

week: represents the day of the week, it can be any integer from 0 to 7, where 0 or 7 represents Sunday.

command: Command to be executed, the system can command, you can also write their own script files.

An asterisk (*): On behalf of all possible values, such as month if the field is an asterisk, it means that the command is executed operation after a month constraints to meet other fields.

Comma (,): a comma-separated values ​​can be used to specify a list of ranges, e.g., "1,2,5,7,8,9."

The bar (-): can represent an integer in the range of a bar between the integer, e.g., "2-6" indicates "2,3,4,5,6."

Forward slash (/): n frequency intervals can slash specified time, for example, "0-23 / 2" is performed once every two hours. At the same time slash can be used with an asterisk, for example * / 10, if used in minute field representing performed once every ten minutes.

Reference:
https://blog.csdn.net/zzq900503/article/details/50723191
https://www.cnblogs.com/zoulongbin/p/6187238.html
https://blog.csdn.net/weixin_41004350/article / the Details / 78,492,367? locationNum = FPS = 10 & 1
---------------------

https://jsczxy2.iteye.com/blog/2170658

Tomcat restart detection and e-mail notification when suspended animation

Detection logic: 3 consecutive access interface provided tomcat, if not three times the response code of 200, the automatic restart tomcat
script content:

#!/bin/bash
source /etc/profile
IP=10.78.1.183
PORT=8114
TOMCAT_DIR=/soft/tomcat_8114
#定义发送邮件函数
function send_mail(){ maillist=( [email protected] ) DATE=`date +'%F %T'` SUBJECT="${IP}的${PORT}端口发生故障,已自动重启!" CONTENT="时间:${DATE}\n主机:${IP}\n事件:${PORT}端口发生重启" for mail in ${maillist[*]};do echo -e "${CONTENT}" | mail -s "${SUBJECT}" $mail done } #对接口访问3次 declare -i NUM=0 for i in `seq 1 3`;do CODE=$(/usr/bin/curl -I -m 10 -o /dev/null -s -w %{http_code}"\n" http://${IP}:${PORT}) if [ $CODE == "000" ];then ((NUM++)) fi sleep 3 done #根据NUM的值来确定是否重启tomcat,NUM=3时进行重启 if [ $NUM == 3 ];then IS_LIVING=`jps -m -l -v | grep "${PORT}" | wc -l` if [ ${IS_LIVING} -eq 1 ];then PID=`jps -m -l -v | grep "${PORT}" | awk '{print $1}'` kill -9 $PID ${TOMCAT_DIR}/bin/startup.sh elif [ ${IS_LIVING} -eq 0 ];then ${TOMCAT_DIR}/bin/startup.sh fi send_mail fi

Schedule: testing every 5 minutes

*/5 * * * * /server/scripts/tomcat/check_8114.sh &> /dev/null

 

Guess you like

Origin www.cnblogs.com/zgq123456/p/10966810.html