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

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