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 &

[root@VM_0_7_centos swoole]# nohup: ignoring input and redirecting stderr to stdout
^C
2.查看記錄  tail -f a.txt
[root@VM_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

[root@VM_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  
[root@VM_0_7_centos swoole]# kill -9 29050

5.內存查看情況
 df -h





 

php查看linux內存使用情況: http://www.cnblogs.com/ylong52/p/5561024.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章