0

I'm trying to check if a Python script is running in PHP, but I've no idea how. I've tried using a database: I added an entry "request" with value response "0" and the Python script had to change that value to "1" if it was online. I couldn't get that to work so now I'm asking here

2
  • Which OS is in use? Commented May 29, 2020 at 0:26
  • I'm using Windows Commented May 29, 2020 at 8:34

1 Answer 1

2

Assuming tasklist is available, this could potentially work: (this example using Windows 10)

I first launched a test2.py script:

enter image description here

The test2.py code is simply running:

from time import sleep

while True:
    print('running');
    sleep(3);

Then using this bit of php code: (I named procCheck.php)

<?php
$tasks = [];

// Use -v to include the window title info:
exec('tasklist -v 2>NUL', $tasks);

// Check the tasks array for the script we seek:
$entry = preg_grep('/(test2\.py)/', $tasks);

if (!empty($entry)) {
    foreach($entry as $value) {
        echo $value . PHP_EOL;
    }
}

Gives this output: (one wrapped line in my console window)

enter image description here

Know that if there are other windows open with same script name within the window title will be found too. For example, I had the test2.py open as the active tab in Notepad++ and that entry was found as well:

enter image description here

Changing to some other file tab in Notepad++ only found the first entry.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.