Php exec()命令带& - php脚本仍悬挂

时间:2016-07-21 19:31:12

标签: php python bash raspberry-pi symfony

我有一个星座,有3个涉及的脚本,用php,shell和python编写。所有这些都是使用最新的raspbian在树莓派B +上运行。

python脚本使用此lib http://github.com/xkonni/raspberry-remote.git切换无线套接字并执行以下操作:

#!/usr/bin/env python
import time
import subprocess
import RPi.GPIO as GPIO
import sys

channelCode = sys.argv[1]
unitCode = sys.argv[2]
duration = sys.argv[3]

# Use the pins as they are on the board.
GPIO.setmode(GPIO.BOARD)
# Pin 18
GPIO.setup(12, GPIO.IN)

while 1:
    if GPIO.input(12) == GPIO.LOW:
        subprocess.call('sudo send ' + channelCode + ' ' + unitCode + ' 1', shell=True)
        time.sleep(float(duration))
        subprocess.call('sudo send ' + channelCode + ' ' + unitCode + ' 0', shell=True)
        # 1 sec sleep to get just one low, instead of multiple because of high sensitivity.
        time.sleep(1)
        # Don't slow down system because of endless loop.
        time.sleep(0.01)

无限循环用于识别光栅中断,并且应该仍然运行,用户通过网络界面中断它。

由于lib需要sudo权限,我创建了一个bash脚本,可以通过www-data用sudo执行(只有这个脚本 - 安全性)。

shell脚本使用无限循环启动python脚本或将其杀死:

#!/bin/bash

# If true, then python script shall be activated, if it is false it should be killed.

if [ $1 = "true" ]
    then 
        python /var/www/html/cat-feeder/app/Resources/pi/light-barrier.py $2 $3 $4 &
fi
if [ $1 = "false" ]
    then
        kill -9 `ps -ef | grep light-barrier.py | grep -v grep | awk '{print $2}'`
fi

exit 0

在php中,shell脚本使用参数执行以杀死python脚本并结束循环或启动脚本:

if ($lightBarrierActive === true) {
    exec('sudo /var/www/html/cat-feeder/app/Resources/pi/catfeeder-sudo-script.sh false &');
} 
else {
    exec('sudo /var/www/html/cat-feeder/app/Resources/pi/catfeeder-sudo-script.sh true 11010 4 1 &');
}

现在问题:带有kill的php exec命令确实可以正常工作。但是exec命令带有用于激活python脚本的shell脚本的参数会导致挂起php脚本。

奇怪的是:使用完全相同的命令从命令行执行shell脚本正在运行!

我尝试使用nohup命令执行,但同样的事情。

我也试过这个:PHP hanging while exec() bash script我认为它不会触发shell脚本,因为它不会产生任何输出。

0 个答案:

没有答案