简体   繁体   English

PHP每n秒运行python脚本

[英]PHP run python script every n seconds

I have a python script that I'd like to run every n seconds. 我有一个想每n秒运行一次的python脚本。 I want to be able to start the script with a PHP page. 我希望能够使用PHP页面启动脚本。 I'd like it to run in the background so that I can visit my page, hit 'go', leave the page, come back in a few hours, and stop the whole thing. 我希望它在后台运行,以便我可以访问我的页面,单击“开始”,离开页面,在几个小时后回来,然后停止整个操作。

I'm trying to build a web interface for capturing a screenshot from a webcam every n seconds so that I can compile the image sequence into a time lapse video later. 我正在尝试构建一个Web界面,以便每n秒从网络摄像头捕获一个屏幕截图,以便以后可以将图像序列编译为定时录像。

The command I'm trying to use is: 我正在尝试使用的命令是:

nohup watch -n20 python imgDownload.py &

This usually works when I enter it directly into the command line, but in PHP when I do 当我直接将其输入命令行时,这通常可以工作,但是在PHP中,当我这样做时

$cmd = 'nohup watch -n20 python imgDownload.py &';
$output = shell_exec($cmd);

or 要么

exec($cmd, $output);

or even 甚至

$cmd = 'nohup watch -n20 python ' . dirname(__FILE__) . '/imgDownload.py &';

it doesn't work. 它不起作用。

I know that shell_exec() works because I can successfully run the following: 我知道shell_exec()可以工作,因为我可以成功运行以下命令:

echo shell_exec("echo 'hi, there'");

it puts 'hi, there' on the page, like it should. 它会在页面上显示“嗨,在那儿”,就像应该这样。

Any ideas why it wouldn't work? 有什么想法为什么不起作用? Is there a better way to do this? 有一个更好的方法吗?

use exec and redirect the output to /dev/null, this will cause php to not wait on the command to finish: 使用exec并将输出重定向到/ dev / null,这将导致php不等待命令完成:

$cmd = 'nohup watch -n20 python imgDownload.py &';
exec($cmd . " > /dev/null &");

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

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