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