Today, the company LD asked to write a small script to monitor whether a process exists and how to exit the script. If it does not exist, send an email to the specified mailbox, I use the running status of the daemon to determine the connection status of the service and display the test result to the standard output.
To facilitate monitoring, use crontab to execute the command every two hours.
Code:
#! /Bin/bash # prompt the user to enter a specified daemon name, assign the variable n_proc # echo "Please input a name of process # Read n_proc # search for the daemon process ID specified in the variable n_proc, and assigned to the variable pidnupidnu =$ (PS ax | grep-V "PS ax" | grep-V grep | grep $ n_proc | awk '{print $1 }') # echo "checking for \" $ n_proc \ ", please wait... "## determine whether the variable pidnu is null. If it is null, the process does not exist. If [-z "$ pidnu"] Then ECHO "the status be stopped... "Mail-s" title "********* @ 163.com </root/Shell/mail.txt exit else echo" the status be running... "fi
Set scheduled task
[[Email protected] ~] # Crontab-E2 ***** sh ~ /Shell/status. Sh (if a scheduled task is used, replace $ n_proc with a specified process)
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/45/CA/wKioL1PrMeHhELbyAAEWJr_yLCw575.jpg "style =" float: none; "Title =" qq20140813173102.png "alt =" wkiol1prmehhelbyaaewjr_ylcw575.jpg "/>
Email sent successfully
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/45/C8/wKiom1PrMMmiHeJoAAGy0MBfPhI906.jpg "style =" float: none; "Title =" qq20140813173331.png "alt =" wkiom1prmmmihejoaagy0mbfphi906.jpg "/>
This article from the "steady happiness" blog, please be sure to keep this source http://yuanye1128.blog.51cto.com/8808749/1539594