Linux OPS Learning shell script monitoring Nginx Service

Source: Internet
Author: User

Rookie learn shell script, hands practiced hand simple small experiment, use shell script to determine if Nginx is normal operation, if not run the Nginx service start up.

First, based on process judgment

1, get the nginx process to determine whether the service starts normally.

ps-ef | grep nginx | grep-v grep | wc-l outputs the number of process rows and then determines whether the service is not started if it is not a description of 2.

grep-v grep is used to rule out the process that you use grep to produce.

2. Shell Script Writing

First define a variable web

#!/bin/bash

web= ' ps-ef |grep nginx|grep-v grep|wc-l '

If [$Web-eq 2];then

echo "You nginx start"

Exit 0

Else

Service Nginx Start

Exit 1

Fi

Define a variable web and then determine if it is not, the service will be started using the services Nginx start.

Second, Port-based judgment

Although the number is obtained, but because the port does not exist, it is empty, so the judgment is to use the judgment of the string

That is, the operator of judgment uses "=" instead of "-eq"

#!/bin/bash

webport= ' netstat-nlt |grep 80|awk ' {print $4} ' | cut-d:-F 2 '

If ["$WebPort" = "];then "

echo "You nginx start"

Exit 0

else

Service Nginx Start

Exit 1

Fi

Of course, there are many ways, but also in continuous learning and groping. (The above script can also be monitored for other services such as databases)


Linux OPS Learning shell script monitoring Nginx Service

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.