E: Could not get lock /var/cache/apt/archives/lock

i wanted to upgrade my system so used a few commands to do that


# apt-get update
# apt-get upgrade
# apt-get dist-upgrade

but because of low space, my vm stopped suddenly/crashed. Now when i opened it again and tried the commands again. i got the error in the heading. The system was not able to get a lock on apt.
There can be other similar lock issues as well which i got on removing the first one.

E: Could not get lock /var/cache/apt/archives/lock
E: Could not get lock /var/lib/apt/lists/lock
E: Could not get lock /var/lib/dpkg/lock

Reason:
So this happens because while i want to run the apt command, but already some process behind the scenes has a lock on the resources and while we are sure that we haven’t run anything now or nothing is being run under the hood, the resources are still locked. That is because the last time when system was halted abruptly, i wasn’t able to completely release the resource and now it still has a lock on it.
But then again, there might actually be some other processes running behind the scenes.

Solution:
1. To close any processes running behind the scenes –
a. search for any running process and kill it:

# ps aux | grep -i apt
# killall apt-get

b. go ahead and remove the loc files

# rm /var/lib/apt/lists/lock
# rm /var/lib/dpkg/lock
# rm /var/cache/apt/archives/lock

c. Configure the dpkg again

# dpkg --configure -a

Problem Sovled!