首页 > 解决方案 > 从 PHP 执行 Python 脚本并保持脚本打开

问题描述

我正在使用运动检测器在树莓派上创建一个项目来发送短信。从 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 &');

    }
?>

以下命令仅适用于命令行。我必须使用 -i 标志,以便会话保持打开状态以检测运动。

python -i /home/pi/project/MotionNotification.py &

所以我想我的问题是从 PHP 运行它(我正在使用 apache2 Web 服务器来托管)。所有文件都有读、写、执行权限。我从 PHP 运行的节点命令可以完美运行,仅供参考。我试图给 apache 网络用户 root 访问权限,但仍然没有运气。

有什么想法我可以试一试吗?谢谢!

标签: pythonphpbashraspberry-piraspbian

解决方案


推荐阅读