The shell script is an operation of the basic skills of personnel, in peacetime work, in order to deal with some anomalies or query data, I often have to write shell scripts automated processing.
Here to share a script I wrote to detect the status of the process and automatically restart, very simple, welcome to use.
The code is as follows:
#!/bin/bash#filename:tomcatcheck.sh# Feature Description: Detects the number of Tomcat processes, 0, automatically restarts the source ~/.bash_profilenum= ' Ps-aef |grep Tomcat |grep- V grep|wc-l ' echo ' ' date ' check process ' number is $num ' # # # ' date ' output log contains time if ["$num"-eq "0"]; Thenservice Tomcat St Art # # # #重启tomcatelif [$num-gt 1]; Thenecho "' Date ' tomcat ' s process more than 1,is $num" Elseecho "' Date ' Tomcat ' s process working normal! ' Fiexit 0
Set up a scheduled task, check the timing, and output the test results to log.
*/10 * * * */home/user/tomcatcheck.sh >>/home/user/tomcatcheck.log 2>&1
There's a little pit. Note that the NUM result in the above script is a string, and a logical judgment should be made by string when making the If judgment.
"Gadget" shell detects process status exception auto restart