Skip to content

Instantly share code, notes, and snippets.

@skarllot
Last active December 24, 2015 19:28
Show Gist options
  • Save skarllot/6850269 to your computer and use it in GitHub Desktop.
Save skarllot/6850269 to your computer and use it in GitHub Desktop.
Create a lockfile and writes PID
import os
import sys
fname = "./lockfile"
if os.path.isfile(fname):
os.remove(fname)
try:
a = os.open(fname, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0644)
os.write(a, str(os.getpid()) + '\n')
os.close(a)
except OSError:
sys.stderr.write("Another instance of this program is running\n")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment