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
Assuming tasklist
is available, this could potentially work: (this example using Windows 10)
I first launched a test2.py
script:
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)
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:
Changing to some other file tab in Notepad++ only found the first entry.
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.