Created
December 13, 2012 21:31
-
-
Save brainsik/4280136 to your computer and use it in GitHub Desktop.
A simple way for Python cron tasks to exit if another process is currently running. Does not use a pidfile.
This file contains 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
import os | |
import subprocess | |
import shlex | |
def bail_if_another_is_running(): | |
cmd = shlex.split("pgrep -u {} -f {}".format(os.getuid(), __file__)) | |
pids = subprocess.check_output(cmd).strip().split('\n') | |
if len(pids) > 1: | |
pids.remove("{}".format(os.getpid())) | |
print "Exiting! Found {} is already running (pids): {}".format( | |
__file__, " ".join(pids)) | |
raise SystemExit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple and elegant approach.