简体   繁体   中英

Python check if a process is running or not , and reexecute

I am trying to create a python script that check if a program is running , if this program is not running this program must be reexecuted automatically with this python script

I found out that processes on unix-based operating systems create a lock file to notify that a program is currently running, at which point we can use os.stat(location_of_file) to check if the file exists to determine if a program is running or not.

Is there a similar lock file created on Windows?

If not what are the various ways in Python by which we can determine if a process is running or not?

I am using python 2.7

This will list all your process and match process by its name.If process found then break loop.

 import psutil
 process = psutil.pids()
 for id in process:
     proc = psutil.Process(id)
     if proc.name() == "some process": # add here your process name
         print("Found")
         break 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM