dongsiju1941 2015-04-26 17:47
浏览 112
已采纳

在后台运行进程挂起PHP页面

I have a PHP page that launches a python script, yet the PHP page hangs until the script finishes. Is there a way to run the script that will not make it hang?

PHP code

The problem occurs in case alarm_on. All other cases work fine.

<?php
session_start();
if(isset($_COOKIE['userME']))
    echo "Hello, ".$_COOKIE['userME'].'<br>';

if(isset($_SESSION['UserData']['Username']) || $passed )// or $logins[$Username] == $Password)
{
if (isset($_POST['submit'])) {
    switch ($_POST['submit']) {
        case 'room_light':
                 echo shell_exec("sudo python /home/pi/Desktop/PiControl/room_light.py");
                break;
        case 'alarm_on':
                echo "alarm started";
                shell_exec("nohup sudo python /home/pi/Desktop/PiControl/motion_sensor.py &");
                echo "alarm on";
                break;
        case 'alarm_off':
                echo "alarm off"; 
                echo shell_exec("sudo pkill -f motion_sensor.py");
                break;
}
}
?>

<form method="post">
<input type="submit" name="submit" value="room_light">
<input type="submit" name="submit" value="alarm_on">
<input type="submit" name="submit" value="alarm_off">
</form>
<?php
}
else
{
    header("location:login.php");
}
?>

motion_sensor.py

import sys
import urllib2, urllib
import RPi.GPIO as GPIO
#setup GPIO using Board numbering. pin physical number corresponds to gpio call
GPIO.setmode(GPIO.BOARD)

pin=37
url="http://MY_IP/test.php"

GPIO.setup(pin, GPIO.IN)

def alarmChange(channel):
    if(GPIO.input(pin)):
        print 'Sensor tripped',channel
        postdata={'sensor_tripped': channel}
        postdata=urllib.urlencode(postdata)
        req=urllib2.Request(url, postdata)
        req.add_header("Content-type", "application/x-www-form-urlencoded")
        page=urllib2.urlopen(req).read()
        #print page
    else:
        print 'sensor no longer tripped', channel

GPIO.add_event_detect(pin, GPIO.BOTH, callback=alarmChange)
print "alarm on"
while True: #this is a hack to keep the script running all the time so it will continue event detection
    i=0

GPIO.cleanup() #should never hit this point
  • 写回答

1条回答 默认 最新

  • doulao7998636570 2015-04-26 17:50
    关注

    Nohup doesn't seem to fix the problem because it still redirects to stdout. I confirmed on my linux dev box.

    What you need to do is redirect the output away from stdout/stderr:

    shell_exec("sudo python /home/pi/Desktop/PiControl/motion_sensor.py >/dev/null 2>&1 &");
    

    By adding >/dev/null you are directing the output to /dev/null. By using 2>&1 you are directing the errors to the same place as the output (/dev/null). You could also use a log file if you want. If you want to append to the file, change > to >>.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办