How to kill the process holding the apt lock

aptkilllockps

I tried doing

sudo apt update

but got:

Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable)

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

I am trying to get the latest version of mongod. Following some instructions I found, I did:

$ ps aux | grep apt
5019  0.0  0.0  14224   980 pts/0    S+   02:52   0:00 grep --color=auto apt

But I don't know which part of this I should plug into

kill -9 processnumber <id>

to make it work.

Which part is the ID and is there any way to prevent this from happening again?

Best Answer

If you want to kill processes based on their name or argument list, use pkill.

pkill regexp

Will kill all processes whose name matches the regexp extended regular expression.

pkill -f regexp

Will kill all processes whose list of arguments (including the first which usually contains the command name) concatenated with spaces matches the regexp.

Here however, it looks more like you want to kill the process(es) that holds the /var/lib/apt/lists/lock lock file, so:

fuser -k /var/lib/apt/lists/lock

(with some fuser implementations) or

lsof -t /var/lib/apt/lists/lock | xargs kill

may be more appropriate.

Though you may want to check what process it is first with lsof /var/lib/apt/lists/lock or fuser /var/lib/apt/lists/lock. And exit it normally if possible instead of coldly kill it.

In any case, avoid kill -9 which doesn't let a chance to the process to exit cleanly.

Related Question