douxidang9092 2018-12-25 14:49
浏览 333
已采纳

PHP - shell_exec似乎有时间限制

I'm running a python script while running a php script online. Because of debugging I made one php script:

<?php
echo shell_exec("python ../reader.py");

If run it (on linux) with php test.php it works and outputs some data. But if I try to access the output on the web, it will give an empty string back.

reader.py takes 8 seconds (so no time limit should be reached).
reader.py:

#!/usr/bin/env python
import sys
import Adafruit_DHT

sensor = Adafruit_DHT.DHT11;
pin = 4;

humiture, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humiture is not None and temperature is not None:
    print("{\"temperature\": " + str(temperature) + ", \"humiture\":"  + str(humiture) + "}")
else:
    print("fail")

Some other commands like ls will work always.

I think there is a problem with the duration but how can I pass this issue?

Solution: Solved my problem giving permissions:

sudo chmod g+s /var/www  
sudo chmod 775 /var/www  
sudo chown -R www-data:www-data /var/www
  • 写回答

1条回答 默认 最新

  • doupi7619 2018-12-26 12:19
    关注

    Chances are that the user your Web server runs as, and whose permissions the script inherits when it is run from a web page, is not allowed to access the hardware, and returns an empty temperature and humidity - or nothing at all.

    Another possibility is that the PHP script executes somewhere else than you think, and finds no reader.py script to launch.

    Try using "ls ../reader.py" to see whether the script is accessible, and run

    "python ../reader.py 2>&1"
    

    to also collect any errors from the python spawning. By the way, is the python executable reachable at all? Have you tried an empty reader2.py that only prints "Hello world"?

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

报告相同问题?

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,