zoukankan      html  css  js  c++  java
  • php调用shell程序出现僵尸程序defunct

    在php中调用bash shell

    system("/home/app/clean_queue.sh", $retval);

    clean_queue.sh如下:

    #!/bin/bash
    sid="/data/httpsqs/queue"
    pid=`ps -ef|grep -v grep|grep $sid|sed -n '1P'|awk '{print $2}'`
    while [ -n "$pid" ] 
    do
    echo "queue id=$pid"
    kill $pid 
    pid=`ps -ef|grep -v grep|grep $sid|sed -n '1P'|awk '{print $2}'`
    done
    
    echo "has shutdown queue."
    rm -rf /data/httpsqs/queue/httpsqs.db
    echo "has delete queue db."
    
    /usr/bin/httpsqs -d -p 1234 -x /data/httpsqs/queue 
    echo "has restarted page queue"
    
    pid2=`ps -ef|grep -v grep|grep $sid|sed -n '1P'|awk '{print $2}'`
    while [ -z "$pid2" ] 
    do
    echo "queue has not been started, ready to retry."
    /usr/bin/httpsqs -d -p 1234 -x /data/httpsqs/queue 
    pid2=`ps -ef|grep -v grep|grep $sid|sed -n '1P'|awk '{print $2}'`
    done
    echo "end check if started"

    但执行的时候此段php代码始终不结束

    [root@script ~]# ps -ef|grep defunct
    root 11273 21369 0 16:58 pts/0 00:00:00 php test_defunct.php
    root 11275 11273 0 16:58 pts/0 00:00:00 [sh] <defunct>
    root 11384 13877 0 16:59 pts/2 00:00:00 grep defunct

    刚开始看到/usr/bin/httpsqs的背景颜色是红色的,以为是权限的问题,后来chmod u-s /usr/bin/httpsqs去掉suid属性还是问题依旧。

    再后来感觉是不是httpsqs启动后是deamon程序,调用的php一直判断这个shell没结果,就一直等,尝试加上nohup后台执行,如下面的写法:

    system("nohup /home/app/clean_queue.sh > /home/app/log/clean_queue 2>&1", $retval);

    果然就不出现僵尸进程了。

  • 相关阅读:
    函数对象与闭包函数
    day15
    应用在tomcat下的四种部署方式(原创)
    Servlet的四种映射模式
    背景框代码
    RabbitMQ 示例-生产者-消费者-direct-topic-fanout
    idea 设置自动生成注释
    cSpring Boot整合RabbitMQ详细教程
    Windows下RabbitMQ安装及配置
    Threadlocal线程本地变量理解
  • 原文地址:https://www.cnblogs.com/jenqz/p/2840952.html
Copyright © 2011-2022 走看看