免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 855 | 回复: 0
打印 上一主题 下一主题

Kill Process [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-24 18:38 |只看该作者 |倒序浏览

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.



  • /**   
    * PHP Kill Process   
  • *   
    * Sometimes, it can happen a script keeps running when it shouldn't, and it   
  • * won't stop after we close the browser, or shutdown the computer. Because it's   
    * not always easy to use SSH there's a workaround.   
  • *   
    * @author      Jensen Somers   
  • * @version     1.0   
    */   
  •    
    class KillAllProcesses {   
  •     /**   
         * Construct the class   
  •      */   
        function killallprocesses() {   
  •         $this->listItems();   
        }   
  •          
        /**   
  •      * List all the items   
         */   
  •     function listItems() {   
            /*   
  •          * PS   Unix command to report process status   
             * -x   Select processes without controlling ttys   
  •          *   
             * Output will look like:   
  •          *      16479 pts/13   S      0:00 -bash   
             *      21944 pts/13   R      0:00 ps -x   
  •          *   
             */   
  •         $output =   shell_exec('ps -x');   
                
  •         $this->output($output);   
                
  •         // Put each individual line into an array   
            $array  =   explode("\n", $output);   
  •             
            $this->doKill($array);   
  •     }   
             
  •     /**   
         * Print the process list   
  •      * @param   string  $output   
         */   
  •     function output($output) {   
            print   "".$output."";   
  •     }   
             
  •     /**   
         * Kill all the processes   
  •      * It should be possible to filter in this, but I won't do it now.   
         * @param   array   $array   
  •      */   
        function doKill($array) {   
  •         /*   
             * Because the first line of our $output will look like   
  •          *        PID TTY      STAT   TIME COMMAND   
             * we'll skip this one.   
  •          */   
            for ($i = 1; $i count($array); $i++) {   
  •             $id =   substr($array[$i], 0, strpos($array[$i], ' ?'));   
                shell_exec('kill '.$id);   
  •         }   
        }   
  • }   
    new KillAllProcesses();   
  • ?>   

    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/59571/showart_1739610.html
  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP