Problem
I am using PHP to interact with a multitude of Python services.
This is how PHP uses bash to start processes and get PID:
$created_pid= shell_exec("nohup python3.8 -u $python_filename >> $log_filename & echo $!");
echo json_encode([
"resolved"=>true,
"pid"=>trim($created_pid)
]);
This perfectly logs the started PID. However, I would like to also write a function that checks if the PID is still active and if it's associated with the correct script contained in $python_filename.
What I tried
So let's suppose $python_filename = "test.py" and $created_pid = 29780
I have tried using this command to retrieve process info based on PID:
$ ps -p 29780
Which outputs:
29780 pts/0 00:00:00 python3.8
Yeah, it's telling me it's a python3.8 process, and I'm happy but not satisfied. I need to know if, in particular, it's the test.py script.
On the other hand, if I execute:
$ ps -ef | grep "python"
Which outputs something that is too hard to parse:
root 29780 29615 0 17:26 pts/0 00:00:00 python3.8 -u test.py
So this is basically close to the solution that I'm expecting. What I'm looking for is just a cleaner output of the last bash command, which only contains test.py.
kill -0 <pid>checks if a process is alive.test.pypython filename running under that process or it's another script :)ps -p 5771 -f | awk 'NR == 2 { print $NF }'where5771is your pid