Check if Python is running with PHP

I have a python script that gets called twice a day with a CRON job to trigger an alarm that runs in a loop and ends by pressing a physical button and calling the destroy function when that button is pressed.

So far so good.

Now I want to check via PHP if this script is running - so before the button is clicked but after it is called via CRON.

How can i do this?

Based on this question - I've tried the following:

exec("jobs", $pids);
if(!empty($pids)) {
    print_r($pids);
}

      

But that doesn't seem to return any value. Calling jobs

from the terminal works as expected and shows the job as being executed when the script is called directly using python3 alarm.py &

.

Usage exec("ps -A | grep -i $processName | grep -v grep", $pids);

I feel it won't work, as the process ID may change and may not be known until the script is run.

+3


source to share


1 answer


Use pgrep

for this:

pgrep -f 'python scriptname.py ...'

      



-f

you can specify the complete command line of a python process to avoid collision with multiple python processes running in parallel.

+1


source







All Articles