I have this code for getting PIDs for specific processes; the code is running well, but sometimes I get this error:
psutil.NoSuchProcess: Process no longer exists (pid=xxxx)
How can this problem be solved? And how can I restart the script if this error or other errors happened?
import psutil
my_pid = None
pids = psutil.pids()
for pid in pids:
ps = psutil.Process(pid)
# find process by .exe name, but note that there might be more instances of solitaire.exe
if "solitaire.exe" in ps.name():
my_pid = ps.pid
print( "%s running with pid: %d" % (ps.name(), ps.pid) )