Process monitoring of Linux shell practice

Time:2020-3-31

Requirements: monitor the system for nginx. Every 3 seconds, the successful output test succeeds, the failed output test fails and restart the nginx service
The code is as follows (for reference only, the case is applicable to the author’s own MBP)

#!/bin/bash
Echo "this is the first case of shell programming."

#Start of monitoring
while true
do
   #PS view status
   Echo "testing, please wait..."
   sleep 1
   echo "..."
   ps -ef | grep nginx | grep -v grep
   if [$? -ne 0]
   then
      Echo "test result: failed"
      Echo "restart..."
      $(brew services start nginx)
      Echo "restart successful..."
   else
      Echo "test result: success"
   fi

   Sleep for 3 seconds
   sleep 3;

done

Recommended Today

PHP Basics – String Array Operations

In our daily work, we often need to deal with some strings or arrays. Today, we have time to sort them out String operation <?php //String truncation $str = ‘Hello World!’ Substr ($STR, 0,5); // return ‘hello’ //Chinese string truncation $STR = ‘Hello, Shenzhen’; $result = mb_ Substr ($STR, 0,2); // Hello //First occurrence of […]