Created
June 18, 2015 14:12
-
-
Save mcansky/07a86eab5213dcf80e46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
''' | |
Check that apt lock is off or on. If on, wait up to 60 seconds before trying | |
again and returning success (if released) or failure. | |
Author: Thomas R. R. Riboulet <[email protected]> | |
''' | |
import logging | |
import json | |
import time | |
import fcntl | |
# Set up logging | |
LOG = logging.getLogger(__name__) | |
def _apt_locked(): | |
''' | |
Try locking the apt lock | |
True if locked | |
False if unlocked | |
''' | |
with open('/var/lib/dpkg/lock', 'w') as handle: | |
try: | |
fcntl.lockf(handle, fcntl.LOCK_EX | fcntl.LOCK_NB) | |
return False | |
except IOError: | |
return True | |
def check_lock(): | |
''' | |
Check lock state and return success if it's free or released under 60 seconds | |
''' | |
if _apt_locked(): | |
LOG.info("APT lock ON, waiting 60 seconds") | |
time.sleep(60) | |
if _apt_locked(): | |
LOG.info("APT lock still ON, exiting") | |
return False | |
LOG.info("APT lock OFF") | |
return True | |
if __name__ == "__main__": | |
print check_lock() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems some kernel/other package updates broke something with lslocks so it took very long to check:
A nice solution I found is checking it with lsof instead:
but for this the package has to be installed first.
So I decided to put two different checks before which programs are setup by default: