I want to check if a specific Python script is running. The script is "EXIF_GUI_EXPERIMENT.py". Running the script below returns nothing.
If I run ps -fA
in the terminal I see the script is running as process "python3 /home/pi/scripts/tkinter/EXIF/EXIF_GUI_EXPERIMENT.py. What is the best way to see if this script is running?
#!/usr/bin/python3\
import subprocess
cmd = ["ps -fA | pgrep "EXIF_GUI_EXPERIMENT.py""]
result = subprocess.run(cmd, capture_output=True, text=True, shell=True)
print (result)
There's too many nested double quotation marks ("
) that give an error.
Something like this should work:
import subprocess
cmd = ['pgrep', '-f', 'EXIF_GUI_EXPERIMENT.py']
result = subprocess.run(cmd, capture_output=True, text=True)
if result.stdout:
print(f"Process is running: {result.stdout}")
else:
print("Process is not running")
(option -f
in pgrep
matches against full argument lists. The default is to match against process names.)
EDIT: following the suggestion in the comment, removed shell=True
that's in general not needed and not only it might pose a security risk, but it can also impact performance because of the extra shell process, and limit portability as the default shell might be different on different operating systems