All pastes #391433 Raw Edit

Stuff

public python v1 · immutable
·published 2007-03-12 03:53 UTC
rendered paste body
#!/usr/bin/python"""lockfile.pyExample Crontab*/5 *   * * * /home/spencersr/.home/bin/lockfile.py unison -auto -batch -servercmd /home/spencersr/.home/bin/lockfile.py unison"""import os, sys, time, md5mycmdline = open("/proc/%s/cmdline" % os.getpid()).readline()lockfile = "/tmp/%s-lockfile-%s" % (os.getenv("USERNAME"), md5.md5(mycmdline).hexdigest())locked = 0for i in range(100):    if locked:        break           try:        if not os.path.exists(lockfile):            open(lockfile, "w").write("%d" % os.getpid())            continue    except:        pass                      try:        if os.path.exists(lockfile):            pid = open(lockfile).readline()            if int(pid) == os.getpid():                locked = 1                continue    except:        pass            try:        if os.path.exists("/proc/%s" % pid):            cmdline = open("/proc/%s/cmdline" % pid).readline()            if not mycmdline == cmdline:                os.remove(lockfile)                continue        else:            os.remove(lockfile)            continue                except:        pass       time.sleep(0.25)if locked:    os.system(" ".join(sys.argv[1:]))        try:        os.remove(lockfile)    except:        pass