和php的getmypid相关的一些函数,挺有用的


<?php

/**
 * Check for a current process by filename
 * @param $file[optional] Filename
 * @return Boolean
 */
function processExists($file = false) {

    $exists     = false;
    $file       = $file ? $file : __FILE__;

    // Check if file is in process list
    exec(“ps -C $file -o pid=”, $pids);
    if (count($pids) > 1) {
        $exists = true;
    }
    return $exists;
}

?>

&lt;?php
/*

mixed getpidinfo(mixed pid [, string system_ps_command_options])

this function gets PID-info from system ps command and return it in useful assoc-array,
or return false and trigger warning if PID doesn't exists

$pidifo=getpidinfo(12345);

print_r($pidifo);

Array
(
    [USER] =&gt; user
    [PID] =&gt; 12345
    [%CPU] =&gt; 0.0
    [%MEM] =&gt; 0.0
    [VSZ] =&gt; 1720
    [RSS] =&gt; 8
    [TT] =&gt; ??
    [STAT] =&gt; Is
    [STARTED] =&gt; 6:00PM
    [TIME] =&gt; 0:00.01
    [COMMAND] =&gt; php someproces.php &gt; logfile
)

*/

//////////////////////////////////////////////

function getpidinfo($pid, $ps_opt="aux"){

   $ps=shell_exec("ps ".$ps_opt."p ".$pid);
   $ps=explode("\n", $ps);
  
   if(count($ps)&lt;2){
      trigger_error("PID ".$pid." doesn't exists", E_USER_WARNING);
      return false;
   }

   foreach($ps as $key=&gt;$val){
      $ps[$key]=explode(" ", ereg_replace(" +", " ", trim($ps[$key])));
   }

   foreach($ps[0] as $key=&gt;$val){
      $pidinfo[$val] = $ps[1][$key];
      unset($ps[1][$key]);
   }
  
   if(is_array($ps[1])){
      $pidinfo[$val].=" ".implode(" ", $ps[1]);
   }
   return $pidinfo;
}

?&gt;

  1. No comments yet.
(will not be published)

  1. No trackbacks yet.