linux命令行下执行php脚本笔记!

2021-12-24  本文已影响0人  DragonersLi

通过命令行执行php脚本php ./watch.php start|stop|reload

[root@xxx]# php ./watch.php start #开启
----------------start-------------------
queue_balance.php 已运行,pid:29862
----------------start-------------------
[root@xxx]# php ./watch.php stop #停止
----------------stop-------------------
queue_balance.php 进程已停止
----------------stop-------------------
[root@xxx]# php ./watch.php reload #重载
#################reload#################
----------------stop-------------------
queue_balance.php 进程已停止
----------------stop-------------------
----------------start-------------------
queue_balance.php 进程本次运行,pid:30275
----------------start-------------------
#################reload#################

#开启状态
[root@xxx]# ps -ef|grep queue_balance.php
root     30275     1  0 17:02 pts/1    00:00:00 /www/server/php/74/bin/php ./queue_balance.php
root     30987 23928  0 17:10 pts/1    00:00:00 grep --color=auto queue_balance.php
[root@xxx]# ps -ef|grep queue_balance.php|grep -v grep
root     30275     1  0 17:02 pts/1    00:00:00 /www/server/php/74/bin/php ./queue_balance.php

#关闭状态
[root@xxx]# ps -ef|grep queue_balance.php
root     31172 23928  0 17:12 pts/1    00:00:00 grep --color=auto queue_balance.php
[root@xxx]# ps -ef|grep queue_balance.php|grep -v grep
[root@xxx]#

#查看日志
[root@xxx]# tail -f ./watch.txt 
2021-12-24 17:02:21-开启进程:queue_balance.php

2021-12-24 17:02:21-0

2021-12-24 17:02:22-1

2021-12-24 17:02:24-2

...

watch.php代码:配置调试模式是否开启,记录日志;设置php路径;日志文件路径;脚本路径等

<?php


class watch{


    private $debug = false;
    private $php = '/www/server/php/74/bin/php';
    private $log_file = './watch.txt';
    private $queue_arr = [
        'queue_balance.php',
    ];


    public function __construct($argv){
        $this->params=$argv;
    }

    public function index(){
        $disable_functions=ini_get('disable_functions');
        if($disable_functions){
            $dis_funs=explode(',',$disable_functions);
            if(in_array('exec',$dis_funs)){
                exit("请修改php.ini配置文件里面的disable_functions项,允许exec函数执行!\n");
            }
        }

        if($this->params[1]=='start'){
            $this->start();
        }elseif($this->params[1]=='stop'){
            $this->stop();
        }elseif($this->params[1]=='reload'){
            $this->reload();
        }else{
            exit("允许命令: php ./watch.php start|stop|reload\n");
        }

    }
    //开始
    private function start(){

        echo "----------------start-------------------\n";
        foreach($this->queue_arr as $qv){
            $pid=$this->getRunningPid($qv);
            if(!$pid){
                $res=$this->exec_php($qv);
                if(!$res){
                    echo $res."\n";
                }else{
                    $pid=$this->getRunningPid($qv);
                    echo "{$qv} 进程本次运行,pid:{$pid}\n";
                }
            }else{
                echo "{$qv} 已运行,pid:{$pid}\n";
            }
        }
        echo "----------------start-------------------\n";
    }

//停止
private function stop(){

        echo "----------------stop-------------------\n";
        foreach($this->queue_arr as $qv){
            $pid=$this->getRunningPid($qv);
            if(!$pid){
                echo "{$qv} 进程已停止\n";
            }else{
                $this->kill_php($qv);
                $pid=$this->getRunningPid($qv);
                if($pid){
                    echo "{$qv} 进程关闭失败\n";
                }else{
                    echo "{$qv} 进程已停止\n";
                }
            }
        }
        echo "----------------stop-------------------\n";
    }

    //重载
    private function reload(){
        echo "#################reload#################\n";
        $this->stop();
        $this->start();
        echo "#################reload#################\n";
    }

    //获取正在执行文件的程序id
    private function getRunningPid($fileName){
        if(!$fileName){
            return false;
        }
        exec("ps -ef|grep '{$fileName}'|grep -v 'grep'",$result);
        if(!$result[0]){
            return false;
        }
        $result_str=preg_replace("/\s(?=\s)/","\\1",$result[0]);
        if(!$result_str){
            return false;
        }
        $resultArr=explode(' ',$result_str);
        return  intval($resultArr[1]);
    }

    //执行php
    private function exec_php($fileName){
        $file='./'.$fileName;
        if(!file_exists($file)){
            return "不存在文件:{$file}";
        }
        $log_file = $this->debug ? $this->log_file : '/dev/null';

        exec("{$this->php} {$file} >>{$log_file} &",$result,$resultInt);
        file_put_contents('./watch.txt',date('Y-m-d H:i:s')."-开启进程:{$fileName}\n\n",FILE_APPEND);
        return true;
    }

    //结束运行
    private function kill_php($fileName){
       $log_file = $this->debug ? $this->log_file : '/dev/null';
       exec("kill {$this->getRunningPid($fileName)} >>{$log_file}");
       file_put_contents('./watch.txt',date('Y-m-d H:i:s')."-结束进程:{$fileName}\n\n",FILE_APPEND);
   }


}


exit((new watch($argv))->index());

queue_balance.php测试代码:

for($i=0;$i<100;$i++){
    sleep($i);
    file_put_contents('./watch.txt',date('Y-m-d H:i:s')."-$i\n\n",FILE_APPEND);
}
上一篇下一篇

猜你喜欢

热点阅读