简体   繁体   中英

Get running status of python program in PHP

Hi I am doing a project with Raspberry Pi. I make a python program which have an endless loop inside. I also make a PHP website which call to that python program, make it run in background by this way:

$call = "sudo python ../python/readver12.py > /dev/null 2>&1 &";
shell_exec($call);

Everything seem okay, but I don't know how to get the status of my python program is running in background or not, and make it available in my website with PHP ?

With shell_exec() PHP will wait to continue your script until the application you're calling exits and will return the output of that application as a string. When your application picks back up the child is already done (or you've hit the PHP time limit ).

It sounds like you want to start the process and monitor it while it's running. For that look at proc_open() and proc_get_status() .

I guess there is many ways to do that:

Try get the logs from terminal You can throw some logs at your terminal while running your endless python script, and capture it with PHP

https://docs.python.org/3/howto/logging.html

Write on files and read with it with php

Straight forward, you write in file with python and read with PHP http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

API REST On PHP website and python with cURL You can use cURL inside your python script to comunicate with your endpoints in php and get the needed data.

cURL: http://pycurl.io/docs/latest/index.html

PHP Api Rest: https://www.codeofaninja.com/2017/02/create-simple-rest-api-in-php.html

I hope it helps

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