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);
果然就不出现僵尸进程了。