On server A, I want to issue the following commands to Server B through ssh.
service network stop
sleep 5
service network start
The problem is because I issued a network 'stop', then my current ssh connection is lost as well. Therefore I cannot execute the succeeding commands (sleep 5 and service network start). Note that I cannot use (service network restart).
Does anyone have a workaround / solution for this?
If you are doing this interactively, why not start a
screen
session? It would look something like this:(scren shell starts)
(SSH session disconnects, but the network restart continues in the screen session)
(Wait a few seconds)
(SSH back into the host once the restart finishes)
(Reconnect to screen and check for errors)
IMHO, it's always scary to restart a network interface remotely. What happens when it doesn't come back up? Do you have a console or other means into the host if something bad happens?
The exact commands available to do this vary based on Linux distribution. On option which is pretty standard is to schedule and "at" job for 5 seconds in the future to restart networking. Another one is to use the
nohup
command.Other distributions have the daemon command to turn the resulting program into a daemon that is no longer associated with the shell.
A very simple way to do this is by using the and operator:
Try this (maybe installing cron if needed):
Then logout, wait 6 min and relogin
This works with modern Debian and Ubuntu, while all the other answers will not work.
Please keep in mind that it can take a little bit to get the interface back. In my case about ~15 seconds as I do have a bond.
Why not put it into a shell script and execute that via SSH?
It sounds like you want either screen or tmux. These will allow you to preserve your session through the loss of a network connection. They're really quite useful, almost all of my terminal sessions are through screen.
e.g.
Generally,
systemctl restart networking
(Debian/Ubuntu),systemctl restart network
(CentOS) should work, without atmux
/screen
session.