简体   繁体   English

在PHP页面上后台运行进程

[英]running process in background on hangs PHP page

I have a PHP page that launches a python script, yet the PHP page hangs until the script finishes. 我有一个启动python脚本的PHP页面,但PHP页面一直挂起,直到脚本完成。 Is there a way to run the script that will not make it hang? 有没有办法运行不会使脚本挂起的脚本?

PHP code PHP代码

The problem occurs in case alarm_on. 万一出现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 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

Nohup doesn't seem to fix the problem because it still redirects to stdout. Nohup似乎无法解决问题,因为它仍重定向到stdout。 I confirmed on my linux dev box. 我在我的linux dev框中确认了。

What you need to do is redirect the output away from stdout/stderr: 您需要做的是将输出重定向到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. 通过添加>/dev/null ,可以将输出定向到/ dev / null。 By using 2>&1 you are directing the errors to the same place as the output (/dev/null). 通过使用2>&1您会将错误定向到与输出(/ dev / null)相同的位置。 You could also use a log file if you want. 如果需要,也可以使用日志文件。 If you want to append to the file, change > to >> . 如果要附加到文件,请将>更改为>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM