I have noticed that when I issue a reboot
command, my SSH connection is immediately closed on CentOS 7. It does not mean the server has rebooted, as I can still ping it.
It seems systemd is killing sshd too early. Sometimes a service will get stuck shutting down or just be slow, and I can't login anymore to verify what is wrong even though the server is up.
Is there a way to tell systemd to only kill sshd after all other service are down?
Even if you could ensure that SSH is the last service to be shut down, it would be pointless, because you will lose connection as soon as network services are stopped.
A Linux machine that boots starts all services in a pre-determined order: first the most vital services, the syslog, the firewall, then the network, and eventually the applications requiring network access (server web, server SSH, etc.). When shutting down, these services are stopped in reverse order. So you will lose SSH connection almost immediately.
The fact that SSH is already down while you can still ping the remote machine is normal.
This is a bit of a hack, but what about adding something like:
to the "[Service]" section of
/usr/lib/systemd/system/sshd.service
? That wouldn't guarantee the ordering of the shutdowns, but if you could tolerate a longer shutdown time, it might allow you to keep your shell active long enough to verify that everything else terminated ok, or to do some quick poking around. Not sure if this is helpful in the general case, as it's probably too late to abort a problematic shutdown at this stage anyway, but it could be useful in a situation where you have a recurring problem during shutdown that you're trying to debug.I am not sure what you are after here but the first thing one will think about is to renice the process.
So in your case you can renice the sshd parent process: