1. 程式人生 > >php 用swoole 實現定時器 執行linux指令碼,檢查程序掛了,重啟操作

php 用swoole 實現定時器 執行linux指令碼,檢查程序掛了,重啟操作

利用swoole的定時器,每兩秒檢查一下

class Grep 
{
    
    const PORT = 9999;
    public function port()
    {
        $shell = "netstat -anp 2>/dev/null | grep ". self::PORT ." | grep LISTEN | wc -l";
        $result = shell_exec($shell);
        if($result != 1){
            //傳送報警
            echo date('Ymd H:i:s').'error'.PHP_EOL;
            //檢測到失敗重啟一下
            $shell = "php /www/web/c_jxx520_xin/public_html/public/index.php index/index/wsServer";
            $shell_result = shell_exec($shell);
        }else{
            echo date('Ymd H:i:s').'SUCCESS'.PHP_EOL;
        }
    }
}

swoole_timer_tick(2000,function($timer_id){
    (new Grep())->port();
    echo "time_start".PHP_EOL;
});

程序掛了,重啟一下就可以了

在linux上執行 php grep.php指令

不用開啟shell視窗,直接指令碼無間斷後臺執行
1.並且記下日記
nohup /www/wdlinux/phps/70/bin/php /www/web/c_jxx520_xin/public_html/swoole/grep.php > /www/web/c_jxx520_xin/public_html/swoole/a.txt &

[[email protected]_0_7_centos swoole]# nohup: ignoring input and redirecting stderr to stdout
^C
2.檢視記錄  tail -f a.txt
[
[email protected]
_0_7_centos swoole]# tail -f a.txt 20181109 00:00:31SUCCESS time_start 20181109 00:00:33SUCCESS time_start 20181109 00:00:35SUCCESS time_start 20181109 00:00:37SUCCESS time_start 20181109 00:00:39SUCCESS 3.查詢下指令碼的開啟情況 ps aux | grep /www/web/c_jxx520_xin/public_html/swoole/grep.php [[email protected]_0_7_centos swoole]# ps aux | grep /www/web/c_jxx520_xin/public_html/swoole/grep.php root 28484 0.0 0.7 270524 13380 pts/12 S 00:00 0:00 /www/wdlinux/phps/70/bin/php /www/web/c_jxx520_xin/public_html/swoole/grep.php root 29050 0.0 0.0 110268 924 pts/12 S+ 00:02 0:00 grep --color=auto /www/web/c_jxx520_xin/public_html/swoole/grep.php 4.關閉程序 kill -9 28484 [
[email protected]
_0_7_centos swoole]# kill -9 29050 5.記憶體檢視情況 df -h

 

php檢視linux記憶體使用情況: http://www.cnblogs.com/ylong52/p/5561024.html