In my Python script, i want to check if otherscript.py is currently being run on the (Linux) system. The psutil library looked like a good solution:
import psutil
proc_iter = psutil.process_iter(attrs=["name"])
other_script_running = any("otherscript.py" in p.info["name"] for p in proc_iter)
The problem is that p.info["name"] only gives the name of the executable of a process, not the full command. So if python otherscript.py is executed on the system, p.info["name"] will just be python for that process, and my script can't detect whether or not otherscript.py is the script being run.
Is there a simple way to make this check using psutil or some other library? I realize i could run the ps command as a subprocess and look for the otherscript.py in the output, but i'd prefer a more elegant solution if one exists.
I wonder if this works
import psutil
proc_iter = psutil.process_iter(attrs=["pid", "name", "cmdline"])
other_script_running = any("otherscript.py" in p.info["cmdline"] for p in proc_iter)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With