[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Can't externally acquire lock on /var/lib/apt/lists/lock



2010/2/10 Jonathan Nieder <jrnieder@gmail.com>:
> I do not know how to use fcntl locks from a shell script.  I would
> suggest trying a scripting language like Perl or Python instead.
>

Ok, the following is fine with me and don't add dependencies except python.

---------------------------------
#!/usr/bin/python

import fcntl, sys, subprocess

try:
    fp = open('/var/lib/apt/lists/lock', 'w')
    fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
    # Unable to acquire the lock for some reason, report it
    # some way and exit
    sys.exit(1)

# Launch shell script 'critical.run' in the critical section
subprocess.call('./critical.run', shell=True)
---------------------------------

It can be launched with "sudo python lock.py". With gksudo is even
more integrated with desktop environments and is possible to do error
reporting in gtk with python. It scatters the whole script in more
files, but at least is correct.

Again, thanks Jonathan for the hint about fnctl().

Regards,
Francesco


Reply to: