Created
October 3, 2019 07:21
-
-
Save palawer/d11a58b74e8dedf946c2f29f323ec6cd to your computer and use it in GitHub Desktop.
Check if python process is already running
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_pid(pid): | |
try: | |
os.kill(pid, 0) | |
except OSError: | |
return False | |
else: | |
return True | |
pid = None | |
try: | |
pid = int(open("script_name.pid").read()) | |
except: | |
pass | |
if pid: | |
if check_pid(pid): | |
logging.info("An instance of script_name is already running: PID={}. Bye".format(pid)) | |
sys.exit(0) | |
pid = os.getpid() | |
open("script_name.pid","w").write(str(pid)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment