PHP实现进程管理

php如何实现简单的进程管理?

  1. 首先定义一个要执行的文件work.php,比较简单,死循环输入当前的时间.当然你也可以自己写一个.
    1
    2
    3
    4
    5
    6
    <?php
    while (true)
    {
    echo date('Y-m-d H:i:s').PHP_EOL ;
    sleep(3);
    }
  2. 开始写进程管理文件workProcess.php,根据自己的需要进行优化.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    <?php
    /**
    * 管理php进程
    */
    class workProcess {
    const PHP_PATH = '/usr/bin/php';
    const PHP_LOG = './php.log';
    private static $process = null;
    //命令必须在这个里面
    private static $cmd_str = array('start','stop','restart');
    //脚本执行次数
    private static $times = 0;
    /**
    * main
    * 主要程序,用来控制php进程开启,结束,重启
    * @param mixed $argv
    * @static
    * @access public
    * @return void
    */
    public static function main($argv) {
    //需要执行的php文件
    $filename = isset($argv[1]) ? $argv[1] : '';
    $cmd = isset($argv[2]) ? $argv[2] : '';
    $num = isset($argv[3]) ? $argv[3] : 0;
    if(empty($filename)) {
    echo "process is not null \n";
    exit;
    }
    if(empty($cmd)) {
    echo "cmd not exists \n";
    exit;
    }
    self::$process = $filename;
    self::$times = $num;
    //检测命令
    if(!in_array($cmd,self::$cmd_str)) {
    echo "cmd is not wrong! \n";
    exit;
    }
    self::$cmd();
    }
    /**
    * start
    * 启动程序
    * @static
    * @access public
    * @return void
    */
    public static function start() {
    for($i=0; $i <=self::$times; $i++ ) {
    self::runWork();
    }
    echo "start done~!\n";
    exit;
    }
    /**
    * stop
    * 终止程序
    * @static
    * @access public
    * @return void
    */
    public static function stop() {
    $pids = self::getPid();
    foreach($pids as $pid){
    self::killWork($pid);
    sleep(1);
    }
    echo "stop done~!\n";
    exit;
    }
    /**
    * restart
    * 重启一个php程序
    * @static
    * @access public
    * @return void
    */
    public static function restart() {
    $pids = self::getPid();
    $count = count($pids);
    for($i=0; $i < $count; $i++ ) {
    self::killWork($pids[$i]);
    self::runWork();
    sleep(1);
    }
    $times = self::$times;
    if( $count < $times) {
    for($j=0; $j< $times-$count; $j++) {
    self::runWork();
    sleep(1);
    }
    }
    echo "restart done~!";
    exit;
    }
    /**
    * runWork
    * 运行程序
    * @static
    * @access private
    * @return void
    */
    private static function runWork() {
    $cmd_str = sprintf("%s %s >> %s &", self::PHP_PATH, self::$process, self::PHP_LOG);
    echo $cmd_str."\n";
    shell_exec($cmd_str);
    }
    /**
    * killWork
    * 杀死一个程序
    * @static
    * @access private
    * @return void
    */
    private static function killWork($pid) {
    $cmd_str = "kill -9 ".$pid;
    shell_exec($cmd_str);
    echo "kill {$pid} \n";
    }
    /**
    * getPid
    * 获取进程id
    * @static
    * @access private
    * @return void
    */
    private static function getPid(){
    $cmd_str = "ps -ef |grep ".basename(self::$process)." |grep -v grep |grep -v ".basename(__FILE__)." |awk '{print $2}'";
    $pid = shell_exec($cmd_str);
    $pids = array_filter(explode("\n",$pid));
    return $pids;
    }
    }
    workProcess::main($argv);
  3. 程序执行

    • 启动5个进程:php workProcess.php work.php start 5
    • 重启进程:php workProcess.php work.php restart
    • 停止进程:php workProcess.php work.php stop

参考来源:
看了阿印的博客,觉得思路不错,自己也模仿写了一个,嘿嘿..