PHP+swoole+linux Implementation System Monitoring and Performance Optimization Operation Example

  • 2021-12-09 08:29:46
  • OfStack

This paper describes the implementation of PHP+swoole+linux system monitoring and performance optimization operations. Share it for your reference, as follows:

Server monitoring

Port Monitor php Run shell Script


class Server {
  const PORT = 8811;
  /**
   *  Get port specified port information ; If you return when running 1 ; Otherwise return 0 ; 
   */
  public function port() {
    $shell = "netstat -anp 2>/dev/null | grep ". self::PORT . " | grep LISTEN | wc -l";
    $result = shell_exec($shell);
    if($result != 1) {
      //  Send alarm service   Mail   SMS 
      /// todo
      echo date("Ymd H:i:s")."error".PHP_EOL;
    } else {
      echo date("Ymd H:i:s")."succss".PHP_EOL;
    }
  }
}
/**
 * swoole Millisecond timer; Every interval 2 Second run 1 Lower script 
 */
swoole_timer_tick(2000, function($timer_id) {
  (new Server())->port();
  echo "time-start".PHP_EOL;
});

linux executes commands at the terminal and writes them to a file

nohup /usr/local/php/bin/php/www/swoole/thinkphp_swoole/script/monitor/server.php > /www/swoole/thinkphp_swoole/script/monitor/a.txt-**

Smooth restart of service using. sh (shell script) based on port alias


echo "loading..."
pid=`pidof live_master`
echo $pid
kill -USR1 $pid
echo "loading success"

Start swoole in the background

nohup /usr/local/php/bin/php /www/swoole/thinkphp_swoole/server/ws.php > /www/swoole/server/thinkphp_swoole/swoole.log &

For more readers interested in PHP related content, please check the topics of this site: "PHP Extended Development Tutorial", "PHP Network Programming Skills Summary", "php curl Usage Summary", "PHP Array (Array) Operation Skills Complete Book", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary" and "php String (string) Usage Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: