Using swoole for system monitoring


In general, we need to monitor a certain service of the server to make sure that the service is running normally. In general, we can use Linux to plan tasks, but the minimum execution time interval of the planned tasks is seconds, which often can not meet our requirements. At this time, the MS timer of swoole should appear

Monitoring code

class Server {
    const PORT = 80;
    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;
// nohup
swoole_timer_tick(2000, function($timer_id) {
    (new Server())->port();
    echo "time-start".PHP_EOL;

Note that the following command requires absolute paths

nohup /usr/bin/php /home/vagrant/code/swoole/script/bin/jian.php > /home/vagrant/code/swoole/script/bin/log.txt &

Additional explanation
When running a job in the foreground, the terminal is occupied by the job; you can add & after the command to run in the background. For example:

sh &

The commands suitable for running in the background are find, time-consuming sorting and some shell scripts. Be careful when running jobs in the background: commands that require user interaction should not be executed in the background, because your machine will wait there. However, the job running in the background will output the results to the screen, interfering with your work. If a job running in the background will generate a lot of output, it is best to redirect its output to a file by using the following methods:

command > out.file 2>&1 &

In this way, all standard and error output will be redirected to a file called out. File.

After the & command is used, the job is submitted to the background for running, and the current console is not occupied, but once the current console is turned off (when exiting the account), the job will stop running. The nohup command can continue to run the corresponding process after you exit the account. No hang up means no hang up. The general form of the command is:

nohup command &

If you submit a job using the nohup command, all the output of the job is redirected to a file named nohup.out by default, unless you specify another output file:

nohup command > myout.file 2>&1 &

After using nohup, many people don’t care. In fact, when the current account exits or ends abnormally, the command will end by itself. Therefore, after using the nohup command to run the command in the background, you need to use exit to exit the current account normally, so as to ensure that the command runs in the background all the time.
2>&1 analysis

command >out.file 2>&1 &

Command > out.file is to redirect the output of command to the out.file file file, that is, the output content is not printed to the screen, but to the out.file file file.
2> & 1 is to redirect the standard error to the standard output, where the standard output has been redirected to the out.file file, that is, the standard error is also output to the out.file. The last &, is to have the command execute in the background.
Imagine what 2 > 1 stands for, 2 and > together stand for error redirection, while 1 stands for error redirection to a file 1 instead of standard output; if 2 > & 1 and 1 together stand for standard output, it becomes error redirection to standard output