Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import print_function
  4.  
  5. import os
  6. import sys
  7. import time
  8. from barman.lockfile import LockFile, LockFileBusy
  9.  
  10. if __name__ == '__main__':
  11.  
  12. if len(sys.argv) != 2:
  13. print('Usage: %s file_to_lock' % sys.argv[0], file=sys.stderr)
  14. sys.exit(2)
  15.  
  16. try:
  17. lock = LockFile(sys.argv[1])
  18. lock.acquire(raise_if_fail=True, wait=False)
  19. pid = os.fork()
  20. if pid == 0:
  21. # Child process: loop endlessly
  22. # detach from parent session
  23. os.setsid()
  24. # close every descriptor different from the locked file
  25. for fd in range(0, 1024):
  26. try:
  27. if fd != lock.fd:
  28. os.close(fd)
  29. except OSError: # ERROR, fd wasn't open to begin with (ignored)
  30. pass
  31. # loop forever
  32. while True:
  33. time.sleep(1)
  34.  
  35. # Parent process: print the pid and exit
  36. print('%d' % pid)
  37. sys.stdout.flush()
  38. # To keep the acquired lock, we don't want any process cleanup
  39. # for the father process. Exit immediately.
  40. # noinspection PyProtectedMember
  41. os._exit(0)
  42. except LockFileBusy:
  43. print('Lock acquisition failed on file "%s"' % sys.argv[1])
  44. sys.stdout.flush()
  45. sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement