2018-12-25 14:49
浏览 276

PHP - shell_exec似乎有时间限制

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

echo shell_exec("python ../");

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. takes 8 seconds (so no time limit should be reached).

#!/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) + "}")

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 script to launch.

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

    "python ../ 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 that only prints "Hello world"?

    打赏 评论

相关推荐 更多相似问题