(Shen Jing) Server Service detection script

Source: Internet
Author: User
Tags arch linux
(Shen Jing) Server Service detection script-Linux Enterprise Application-Linux server application information. The following is a detailed description. [I = s] This post was last edited by Dongdong at, January 10 ,.

Visit my blog http://sillydong.com to see more my original articles

The script provided in this article is still prepared for the server because it has been working on the server for a long time and has not brought about Arch Linux. If I write too much shell, I forget how to write C. That's not good ......

The company's servers often get down for some reason. It's good to go over ssh. Let's see why the process finds that the service is not opened. Why? The server has been restarted! My predecessor left a check. sh script is used to monitor apache and other processes. The script restarts the server when many processes are detected. As a result, some servers do not set up to start those apache at startup, which is a tragedy. I re-wrote a script to replace the original check in crontab. sh: when there were too many processes, it wasn't about restarting the server, but about restarting the service. Now the server is much more normal. The website is responsible for shouting "XXXX website is down. What's the problem?" No, haha.

A little more nonsense. The script is as follows. before using it, please be sure to read the code and modify some of the paths to ensure that no unnecessary harm will be caused on your server.
CODE :#! /Bin/bash
# Service --> name to test
# Apache --> apache | apache2 | http | httpd
# Nginx --> nginx
# Mysql --> mysql | mysqld
# Php-cgi --> php-cgi
# Vsftpd --> vsftp | vsftpd
# Pure-ftpd --> pure-ftp | pure-ftpd
# Apache-tomcat --> java
# Nrpe --> nrpe
# Set the service name you want to test and its max number of processes first.
# Don't forget to check the bin path for each service.
# By Chen. Zhidong
# Http://sillydong.com

PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
Export PATH

SERVICE = "httpd mysqld vsftpd nrpe"
SERVICE_MAX = (70 5 5 1)

LOGPATH = "/root"
APACHE_PATH = "/usr/local/apache2"
NGINX_PATH = "/usr/local/nginx"
PHP_PATH = "/usr/local/php"
TOMCAT_PATH = "/usr/local/apache-tomcat-6.0.26"
PURE_PATH = "/usr/local/pureftpd"
NRPE_PATH = "/usr/local/nagios"
I = 0

If [-e $ LOGPATH/restart. log]
Then
Tail-n 40 $ LOGPATH/restart. log> $ LOGPATH/restart. tmp
Rm-rf $ LOGPATH/restart. log
Mv $ LOGPATH/restart. tmp $ LOGPATH/restart. log
Fi

For serv in $ SERVICE
Do
Counter = 0
Counter = 'ps-A | grep "$ serv" | wc-l'

If [$ counter-eq 0]; then
Echo "$ serv is off at $ (date), starting..."> $ LOGPATH/restart. log
Case $ serv in
Apache * | http *)
# Echo "apache start"
$ APACHE_PATH/bin/apachectl restart> $ LOGPATH/restart. log
;;
Nginx)
# Echo "nginx start"
$ NGINX_PATH/sbin/nginx-s reopen> $ LOGPATH/restart. log
;;
Mysql *)
# Echo "mysql start"
# In general, you should follow the unified standards when setting up the server. Here, you can modify it according to the actual situation.
If [-e/etc/init. d/mysqld]; then
/Etc/init. d/mysqld restart> $ LOGPATH/restart. log
Else
/Etc/init. d/mysql restart> $ LOGPATH/restart. log
Fi
;;
Php-cgi)
# Echo "php-cgi start"
$ PHP_PATH/sbin/php-fpm restart> $ LOGPATH/restart. log
;;
Vsftp *)
# Echo "vsftpd start"
/Etc/init. d/vsftpd restart> $ LOGPATH/restart. log
;;
Pure-ftp *)
# Echo "pureftpd start"
$ PURE_PATH/sbin/pure-ftpd-B> $ LOGPATH/restart. log
;;
Java *) # apache-tomcat
# Echo "tomcat start"
$ TOMCAT_PATH/bin/startup. sh> $ LOGPATH/restart. log
;;
Nrpe)
# Echo "nrpe start"
$ NRPE/bin/nrpe-c $ NRPE/etc/nrpe. cfg
;;
*)
Echo "Wrong service name while starting..."> $ LOGPATH/restart. log
;;
Esac
Else
Echo "$ serv is on at $ (date), next service..."> $ LOGPATH/restart. log
Fi

If [$ counter-gt $ {SERVICE_MAX [I]}]; then
Echo "$ (date), too restart $ serv, needs to restart..." >>$ LOGPATH/restart. log
Case $ serv in
Apache * | http *)
# Echo "apache restart"
$ APACHE_PATH/bin/apachectl restart> $ LOGPATH/restart. log
;;
Nginx)
# Echo "nginx restart"
$ NGINX_PATH/sbin/nginx-s reload> $ LOGPATH/restart. log
;;
Mysql *)
# Echo "mysql restart"
If [-e/etc/init. d/mysqld];
Then
/Etc/init. d/mysqld restart> $ LOGPATH/restart. log
Else
/Etc/init. d/mysql restart> $ LOGPATH/restart. log
Fi
;;
Php-cgi)
# Echo "php-cgi restart"
$ PHP_PATH/sbin/php-fpm restart> $ LOGPATH/restart. log
;;
Vsftp *)
# Echo "vsftpd restart"
/Etc/init. d/vsftp * restart> $ LOGPATH/restart. log
;;
Pure-ftp *)
# Echo "pureftpd restart"
$ PURE_PATH/sbin/pure-ftpd-B> $ LOGPATH/restart. log
;;
Java *) # apache-tomcat
# Echo "tomcat restart"
$ TOMCAT_PATH/bin/startup. sh> $ LOGPATH/restart. log
;;
Nrpe)
# Echo "nrpe start"
Killall nrpe
$ NRPE/bin/nrpe-c $ NRPE/etc/nrpe. cfg-d
;;
*)
Echo "Warning: Wrong service name while restartiing..."> $ LOGPATH/restart. log
;;
Esac
Fi
Let "I ++"
Done

Exit 0

# End

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.