読者です 読者をやめる 読者になる 読者になる

psによる重複チェック

<?php
// 一文字目が.でないなら
if(1){
    //$class_name = preg_quote('/var/spool/clientmqueue', '/');
    $class_name = preg_quote('/var/spool/clientm', '/');
    $file_name =  preg_quote('/var/run/ntpd.pid', '/');
} else {
    $class_name = 'Test_Batch';
    $file_name =  preg_quote('./batch.php', '/');
}

$result = shell_exec('ps ax');
$process_list = explode("\n", trim($result));

//print_r($process_list);

//$process_list = array(
//'header',
//'...run_batch.php Test_Batch_AAA',
//'...run_batch.php Test_Batch_grep',
//'...run_batch.php Test_Batch',
//'...run_batch.php AAA_Test_Batch',
//'...run_batch.php ./batch_aaa.php',
//'...run_batch.php ./batch_grep.php',
//'...run_batch.php ./batch.php',
//'...run_batch.php ./aaa_batch.php',
//);

$match_cnt = 0;
foreach($process_list as $k => $ps){
    if($k == 0){
        continue;
    }
    //print_r(preg_split("/[\s\t]+/", trim($ps)));
    foreach(array($class_name, $file_name) as $name){
        //echo $name." ".$ps."\n";
        //if(preg_match("/^.*\s+{$name}[\s$]/", $ps)){
        if(preg_match("/^.*[\s]+{$name}(.*)$/", $ps, $match)){
            //print_r($match);
            if(strlen($match[1]) && strpos($match[1], ' ') !== 0){
                continue;
            }
            echo "**** match $ps ****\n";
            $match_cnt++;
        }
    }
}

if($match_cnt >= 2){
    echo "**** dup ****\n";
}