简体   繁体   中英

PHP run python script every n seconds

I have a python script that I'd like to run every n seconds. I want to be able to start the script with a PHP page. 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.

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

$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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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