What to do after terminal crashed during apt-get upgrade

No panic everyone, stay calm!

Just recently I have created my own personal home server using the newest Raspberry Pi 3 model B. Its not running on Ubuntu (like I’m used to), but Raspbian Jessie Lite (Raspbian is a free operating system based on Debian optimized for the Raspberry Pi hardware). I have installed NGINX on it, the latest version of PHP5-FPM (no php7 yet) and of course MariaDB (drop-in replacement for MySQL). Today I was doing some routine server maintenance as there were quite a few packages that needed upgrading, including the (for me) important PHP5-FPM as I use WordPress for most of my sites. Unfortunately my terminal crashed right in the middle of the upgrade proces. Packages were already retrieved and configuration was going full speed ahead == disaster!

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get clean
sudo apt-get autoclean
sudo apt-get autoremove

After reopening my terminal I quickly tried to apt-get again. Unfortunately, I encountered the following two errors:

E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

Oi, not so good. I did not want to reboot this server as it was my home server and I was doing some testing on it. In the meanwhile I was receiving numerous emails from Uptime Robot that sites were going down, so I had to hurry!

None of the apt-get commands were working anymore, so I figured that apt-get was probably still running. You can use the following command to check.

ps -a | grep apt-get

It was indeed running and I copied the process id, because in the next step this process is killed. We include -9 as an option (signal number) as this means “forced termination; cannot be trapped”.

sudo kill -9 PROCESSID

Now I could run the upgrade again and Uptime Robot was already sending me notification that the sites were back up.

sudo apt-get upgrade

Whereafter the server told me that I should first run the following commands to complete the configuration which was apparently on hold.

dpkg --configure -a

After this I cleaned and autoremoved apt-get and everything kept running smoothly. So if your terminal ever crashes during an upgrade, don’t worry, simply kill the process and run the upgrade again. No need for a reboot (which might not even solve your issue!).

1 Comment.

  1. Vinay Deolalikar

    Thank you! This just happened to me, and I found your solution which saved me a lot of trouble.

Leave a Comment.