php 用swoole 实现定时器 执行linux脚本,检查进程挂了,重启操作

4 篇文章 0 订阅

利用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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值