Keep ssh running as long as possible on server shutdown

I have remote linux server with paid kvm and very slow 'reboot on request' service. Now when I reboot server with 'sudo reboot' ssh disconnects me within 3-5 seconds, but then server sometimes continue running, I guess, waiting for some rc.d script shutting down. Network still configured, so I can even ping server, but ssh connections can't be established, so, apparently, sshd is down. I can't even dig inside to detect, what is going on during shutdown. So, the question is: how to prevent sshd from stopping during system shutdown? I want sshd running until total system poweroff.


You don't.

Your operating system needs to stop all processes prior to shutting down. That includes killing off your SSH connections.
The network being "alive" is an artifact of the kernel not having halted/restarted yet - Your ping is being answered by the IP/ICMP stack. Userland could be completely gone and the kernel would continue answering ping.


You can reorder the shutdown scripts so sshd is killed later in the process (look into the documentation for your distribution), however this will just keep the SSH daemon running longer - it won't save your session, which was killed as an independent process (this is why you can stop/start sshd while you're still logged in - the active connections are not "part of the daemon" anymore).
If your system is behaving normally it will have disallowed logins once it began the shutdown process, and you won't be able to log in after your session is killed anyway.