Quantcast
Viewing latest article 6
Browse Latest Browse All 30

A simple way for Python cron tasks to exit if another process is currently running. Does not use a pidfile.

gistfile1.py
importos
importsubprocess
importshlex
defbail_if_another_is_running():
cmd=shlex.split("pgrep -u {} -f {}".format(os.getuid(), __file__))
pids=subprocess.check_output(cmd).strip().split('\n')
iflen(pids) >1:
pids.remove("{}".format(os.getpid()))
print"Exiting! Found {} is already running (pids): {}".format(
__file__, " ".join(pids))
raiseSystemExit(1)

Viewing latest article 6
Browse Latest Browse All 30

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>