Skip to content

Instantly share code, notes, and snippets.

@gyoza
Created April 26, 2018 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyoza/ca10b504a7a4f3aa2cdcae2f57faa1a2 to your computer and use it in GitHub Desktop.
Save gyoza/ca10b504a7a4f3aa2cdcae2f57faa1a2 to your computer and use it in GitHub Desktop.
lockfiles
def touch(path):
with open(path, 'a'):
os.utime(path, None)
def lock_file(LOCK_FILE):
now = datetime.now()
restart_hours = [1]
if now.hour in restart_hours:
if VERBOSE: PrettyLogger(log_level="info", msg="H|Restart hour hgreen|Found.")
if (now.minute <= 0 or 59):
if VERBOSE: PrettyLogger(log_level="info", msg="H|Restart minute hgreen|Found.")
if os.path.isfile(LOCK_FILE):
LOCK_FILE_time = int(getmtime(LOCK_FILE))
now = int(time.time())
file_age = now - LOCK_FILE_time
if file_age > 39600:
PrettyLogger(log_level="info", msg="H|Restart file older than 10 hours, hgreen|RESTARTING")
touch(LOCK_FILE)
shell_all = 'echo dostuff'
return_code = subprocess.call(shell_all, shell=True)
else:
PrettyLogger(log_level="info", msg="H|Restart less than 10 hours old. No restart required.".format(file_age))
else:
PrettyLogger(log_level="info", msg="H|No restart file found, hgreen|RESTARTING")
touch(LOCK_FILE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment