SSH connection lost during server upgrade - How to reconnect to process?

Solution 1:

The process actually runs in a screen or byobu session as the root user. reconnect to the server with putty on port 22 or the failsafe 1022. sudo su - or su - into your root account and resume the screen with byobu or a screen -r and resume where you left.

oh and yes i found out the hard way ;-)

Solution 2:

You can't. Find the upgrade process by doing

lsof /var/lib/dpkg/lock

Kill it, fix any interrupted upgrades with

dpkg --configure --pending

and restart the upgrade.

Solution 3:

cryopid (found via this question) might help unless the current "one process, not a tree of processes" is a problem in this instance.

The files may be locked because the process was unceremoniously killed and didn't get chance to clean up after itself, so confirm that aptitude is actually still running. Use lsof /var/lib/dpkg/lock as suggested in Teddy's answer to check - this will tell you the pid to try cryopid on too.

In future I recommend to try running through screen where-ever possible. I have made a habit of screen (or byobu, which is a wrapper around screen that defines some useful behaviours) being the first thing I run when SSHing to anywhere then I can reconnect if there are issues. You can set it up to start or reattach automatically, though I've never got around to doing that anywhere - there are a number of examples online (this one for instance) if you want to try that.