Through the ps – ef | grep tomcat to determine whether a process
1. Put the Tomcat path to be checked in a TXT file, and write tomcat in a row. The name is demo.txt
/home/app/tomcat_8555_pis
/home/app/tomcat_test8556
/home/app/tomcat_test8557
/home/app/tomcat_test8558
/home/app/tomcat_test8559
/home/app/tomcat_test8560
test_not_found
test_not_found
Copy the code
2. Write the demo.sh script
#! /bin/bash -v
Get the default parameter value
echo 'The input file parameter is :' The $1
file_name="The $1"
if [ -z "${file_name}" ];then
file_name="$0"
file_name="${file_name%*.sh}.txt"
fi
echo "The actual file parameters are:${file_name}"
Check whether the process exists
Display OK if it exists, display NG if it does not exist
function check_info(){
processString="The $1"
echo "$(date '+%Y-%m-%d %H:%M:%S')Check the string:$processStringStart -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"
ps -fe | grep "$processString" | grep -v grep
if [ $? -eq 0 ]; then
echo -e "$(date '+%Y-%m-%d %H:%M:%S')Check the string:${processString}\033[42;34m Result :OK \033[0m"
else
echo -e "$(date '+%Y-%m-%d %H:%M:%S')Check the string:${processString}\033[41;30m Result :NG \033[0m"
echo $processString >> "${file_name}.error.log"
fi
# print details
echo "$(date '+%Y-%m-%d %H:%M:%S')The process details are as follows :----------------"
ps -fe | grep "$processString"
ps -fe | grep "$processString" > "${file_name%.sh}.error.log"
echo "$(date '+%Y-%m-%d %H:%M:%S')Process details End :----------------"
echo "$(date '+%Y-%m-%d %H:%M:%S')Check the string:$processStringThe end -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"
echo ' '
echo ' '
}
# exit
echo 'Start execution' > "${file_name}.error.log"
cat "${file_name}" | while read line
do
if [ -n "$line" ];then
echo $line
check_info $line
fi
done
Copy the code
3. Start
sh -v ./demo.sh demo.txt
sh -v ./demo.sh 2>&1 | tee demo.log
Tee command The tee command reads data from the standard input device, outputs its content to the standard output device, and saves it as a file.