0

Each time I try to update my Kali Linux by running

sudo apt update

I get the following error message

Unable to lock directory /var/lib/apt/lists/

The way I fix this problem is by finding processes by running

lsof /var/lib/dpkg/lock
lsof /var/lib/apt/lists/lock
lsof /var/cache/apt/archives/lock

and killing the processes that have acquired the locks.

I would like to automate the process.

2

Unable to lock directory /var/lib/apt/lists/

apt provide apt-daily-upgrade.service and apt-daily.service services , when enabled the apt will be executed in background. see systemctl list-timers

to disable it :

systemctl disable apt-daily.timer
systemctl disable apt-daily.service
systemctl disable apt-daily-upgrade.timer
systemctl disable apt-daily-upgrade.service

Debian: UnattendedUpgrades

0

The answer to the question you should have asked

This is not a good idea. You should not kill processes blindly. Find out which process has the file locked and why.

Furthermore, if you're having this problem often, you're probably doing something wrong. Kali Linux is a special-purpose distribution for penetration testing. It is not designed to be installed. To do anything other than penetration testing, do not use Kali. Use a normal distribution such as Ubuntu.

How to find a process that has a file open in Python

You can use the psutil package to enumerate processes and enumerate the files that they have open. https://stackoverflow.com/questions/20106220/check-for-open-files-with-python-in-linux has a code example.

To just enumerate processes that have a file open, it may be easier to parse the output of fuser.

import os, subprocess
try:
    pids = subprocess.check_output(['fuser', '--', filename], stdout=open(os.devnull))
    for pid in map(int, pids.split()):
       print('Process {} has {} open'.format(pid, filename))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.