Shell script: monitors the Service Process status.

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.