#!/usr/bin/python """homeunison.py""" import os, sys, time lockfile = "/tmp/homeunison.lock" locked = 0 for i in range(100): if locked: break try: if not os.path.exists(lockfile): open(lockfile, "w").write("%d" % os.getpid()) print i, "Wrote pid to file" continue except: pass try: if os.path.exists(lockfile): pid = open(lockfile).readline() if int(pid) == os.getpid(): locked = 1 print i, "Initiated lock" continue except: pass try: if os.path.exists("/proc/%s" % pid): cmdline = open("/proc/%s/cmdline" % pid).readline() if not "homeunison" in cmdline: os.remove(lockfile) print i, "Removed stale lockfile" continue else: os.remove(lockfile) print i, "Removed stale lockfile" continue except: pass time.sleep(0.15) if locked: print os.getpid(), "Wootadoo" time.sleep(30) try: os.remove(lockfile) except: pass