从PHP执行Python脚本并保持脚本打开

从PHP执行Python脚本并保持脚本打开,python,php,bash,raspberry-pi,raspbian,Python,Php,Bash,Raspberry Pi,Raspbian,我正在创建一个关于树莓pi的项目,使用运动检测器发送文本消息。从PHP(Apache2)执行python脚本时遇到一些问题 正在尝试执行此python脚本: #!/usr/bin/env python from gpiozero import MotionSensor import time import os #Motion sensor in GPIO23 ms = MotionSensor(23) def send_notification(): #Debug Motion St

我正在创建一个关于树莓pi的项目,使用运动检测器发送文本消息。从PHP(Apache2)执行python脚本时遇到一些问题

正在尝试执行此python脚本:

#!/usr/bin/env python
from gpiozero import MotionSensor
import time
import os
#Motion sensor in GPIO23
ms = MotionSensor(23)

def send_notification():
    #Debug Motion Statement
    print("There was a movement!")

    #Notification script is stored in notification variable
    notification = os.popen ('bash /home/pi/project/notification.sh')

    #Send Notification
    print(notification.read())

    #Delay between notifications
    time.sleep(15)

#Execute send_notification function on motion
ms.when_motion = send_notification
notification.sh脚本以供参考。它使用后缀发送文本消息

echo "Motion was detected in your Smart Mailbox!" | mail myphonenumber@vtext.com
从以下PHP代码:

<!DOCTYPE html>
<html>

<head>
    <title>Smart Mailbox</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>

<div class="form-style-5">
    <body style="text-align:center;">
        <h1>Smart Mailbox Application</h1>
        <p>Enter Your Phone Number:</p>
        <form method="post">
            <input type="tel" name="phonenumber" placeholder="10 Digit Phone Number" pattern="[0-9]{10}" required>
            <br>
            <p>Select Your Phone Carrier:</p>
            <select name="carrierdropdown" required>
                <option value=""></option>
                <option value="Verizon">Verizon</option>
                <option value="ATT">ATT</option>
                <option value="Sprint">Sprint</option>
                <option value="TMobile">T-Mobile</option>
            </select>
            <br><br>
            <input type="submit" name="submit" value="Start SmartMailbox">
        </form>

    </body>
</div>

</html>

<?php

    if(isset($_POST['submit'])) {
        //Appends phone carrier address to email address
            if($_POST['carrierdropdown']=="Verizon"){
                //echo "DEBUG:Verizon";
                $carrier = "@vtext.com";
            } elseif ($_POST['carrierdropdown']=="ATT"){
                //echo "DEBUG:ATT";
                $carrier = "@txt.att.net";
            } elseif ($_POST['carrierdropdown']=="Sprint"){
                //echo "DEBUG:Sprint";
                $carrier = "@messaging.sprintpcs.com";
            } elseif ($_POST['carrierdropdown']=="TMobile"){
                //echo "DEBUG:T-Mobile";
            } else{
                echo "Break";
            }

        //Gets users phone number from HTML form
        $phonenumber = $_POST['phonenumber'];

        //Opens notification script for editing
        $file = fopen('/var/www/html/notification.sh','w');

        //Writes mail command to file appending phonenumber
        fwrite($file,'echo "Motion was detected in your Smart Mailbox!" | mail ' . $phonenumber . $carrier);

        //Closes file
        fclose($file);

        //Runs python scripts
        exec('node /home/pi/project/blynkconnect.js');
        exec('python -i /home/pi/project/MotionNotification.py &');

    }
?>
所以我想我的问题是从PHP运行它(我使用apache2 web服务器来托管)。所有文件都具有读取、写入和执行权限。我在PHP中运行的node命令可以完美地工作,仅供参考。我已经尝试给apache web用户root访问权限,但仍然没有成功


我能试试看吗?谢谢

你知道PHP理论上会在每次页面加载时运行你的python脚本,而不管实例是否还在运行吗?这不是什么大问题,它仍然可以满足我的要求。在未来,我会尝试找到一个解决方案,在点击按钮时终止进程。尝试检查www数据是否可以运行它(做一些日志记录)您是否知道PHP理论上会在每个页面加载时运行您的python脚本,而不管实例是否仍在运行?这不是什么大问题,它仍然可以满足我的要求。在未来,我会尝试找到一个解决方案,在点击按钮时终止进程。尝试检查www数据是否可以运行它(做一些日志记录)
python -i /home/pi/project/MotionNotification.py &