Skip to content

Instantly share code, notes, and snippets.

@jossef
Last active March 3, 2023 13:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jossef/8979d859de9dc163aa5ecafa6216859d to your computer and use it in GitHub Desktop.
Save jossef/8979d859de9dc163aa5ecafa6216859d to your computer and use it in GitHub Desktop.
python run once
import sys
import fcntl
import os
_run_once_file_handle = 0
def run_once():
global _run_once_file_handle
host_script_file_path = os.path.realpath(sys.argv[0])
_run_once_file_handle = open(host_script_file_path, 'r')
try:
fcntl.flock(_run_once_file_handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
except:
sys.exit('Another instance already running')
import common
import time
common.run_once()
print ('sleeping')
time.sleep(10)
print ('done')
@dachshund-digital
Copy link

This does not appear to work on Debian 9 stretch. Both instances of script run and both acquire lock. I am executing the scripts from different SSH sessions.

@gimli2
Copy link

gimli2 commented Mar 3, 2023

The same behaviour experienced od Debian 11 as dachshund-digital described for Deb9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment