Example of PHP + swoole + Linux for system monitoring and performance optimization

Time:2020-2-11

The example of this paper describes the implementation of PHP + swoole + Linux system monitoring and performance optimization operation. To share with you for your reference, as follows:

Server monitoring

Port monitoring PHP running shell script

class Server {
  const PORT = 8811;
  /**
   *Get the port information specified by the port. If it is running, return 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 email and SMS
      /// todo
      echo date("Ymd H:i:s")."error".PHP_EOL;
    } else {
      echo date("Ymd H:i:s")."succss".PHP_EOL;
    }
  }
}
/**
 *Swoole millisecond timer; run script every 2 seconds
 */
swoole_timer_tick(2000, function($timer_id) {
  (new Server())->port();
  echo "time-start".PHP_EOL;
});

Linux executes commands on 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-**

Use. Sh (shell script) to restart the service smoothly according to the 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 information about PHP, you can see the following topics: PHP extended development tutorial, PHP network programming skills summary, PHP curl usage summary, PHP array operation skills Collection, PHP data structure and algorithm tutorial, PHP programming algorithm summary and PHP string Usage Summary

I hope that this article will be helpful for PHP programming.