Chinaunix首页 | 论坛 | 博客
  • 博客访问: 540798
  • 博文数量: 166
  • 博客积分: 4038
  • 博客等级: 上校
  • 技术积分: 1115
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-14 23:29
文章分类

全部博文(166)

文章存档

2010年(12)

2009年(126)

2008年(28)

分类: LINUX

2008-12-24 18:38:43

It's not the very best solution, but I've used it a couple of times when
I needed to do it quick without to much trouble. Make not I kill all the
processes, on my server px -x will only return like 4 times /sbin/apache
and it's pretty safe to kill them without any trouble.
 

 
  1. /**  
  2.  * PHP Kill Process  
  3.  *  
  4.  * Sometimes, it can happen a script keeps running when it shouldn't, and it  
  5.  * won't stop after we close the browser, or shutdown the computer. Because it's  
  6.  * not always easy to use SSH there's a workaround.  
  7.  *  
  8.  * @author      Jensen Somers   
  9.  * @version     1.0  
  10.  */  
  11.   
  12. class KillAllProcesses {   
  13.     /**  
  14.      * Construct the class  
  15.      */  
  16.     function killallprocesses() {   
  17.         $this->listItems();   
  18.     }   
  19.         
  20.     /**  
  21.      * List all the items  
  22.      */  
  23.     function listItems() {   
  24.         /*  
  25.          * PS   Unix command to report process status  
  26.          * -x   Select processes without controlling ttys  
  27.          *  
  28.          * Output will look like:  
  29.          *      16479 pts/13   S      0:00 -bash  
  30.          *      21944 pts/13   R      0:00 ps -x  
  31.          *  
  32.          */  
  33.         $output =   shell_exec('ps -x');   
  34.             
  35.         $this->output($output);   
  36.             
  37.         // Put each individual line into an array   
  38.         $array  =   explode("\n"$output);   
  39.             
  40.         $this->doKill($array);   
  41.     }   
  42.         
  43.     /**  
  44.      * Print the process list  
  45.      * @param   string  $output  
  46.      */  
  47.     function output($output) {   
  48.         print   "
    ".$output."
    "
    ;   
  49.     }   
  50.         
  51.     /**  
  52.      * Kill all the processes  
  53.      * It should be possible to filter in this, but I won't do it now.  
  54.      * @param   array   $array  
  55.      */  
  56.     function doKill($array) {   
  57.         /*  
  58.          * Because the first line of our $output will look like  
  59.          *        PID TTY      STAT   TIME COMMAND  
  60.          * we'll skip this one.  
  61.          */  
  62.         for ($i = 1; $i < count($array); $i++) {   
  63.             $id =   substr($array[$i], 0, strpos($array[$i], ' ?'));   
  64.             shell_exec('kill '.$id);   
  65.         }   
  66.     }   
  67. }   
  68. new KillAllProcesses();   
  69. ?>   
阅读(827) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~