Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
4  
why wouldn't you just use the "restart" option? why separately stop, and then start? –  SpacemanSpiff Jun 10 '11 at 3:39
add comment

migrated from stackoverflow.com Jun 9 '11 at 17:09

This question came from our site for professional and enthusiast programmers.

6 Answers

If you are doing this interactively, why not start a screen session? It would look something like this:

screen

(scren shell starts)

service network restart

(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)

screen -r

(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?

share|improve this answer
1  
I always operate through screen when working on remote machines. In case of unplanned disconnections it can be a life-saver. Also having several active shells in the same session can be handy. You might also like to look at tmux too, I've not used it myself but some prefer it to screen and its core function provides the same benefits. –  David Spillett Jun 11 '11 at 14:51
add comment

Why not put it into a shell script and execute that via SSH?

share|improve this answer
    
If I put it in a script, then this will happen: on Server A, ssh server_B "execute_script". But won't I still lose my ssh connection from A to B? I am hoping that I can still preserve the ssh connection... –  Carmen Jun 7 '11 at 23:49
1  
You can't preserve the SSH session if you're stopping the network interface on a server. –  Christopher Armstrong Jun 7 '11 at 23:55
    
so, it would look like my ssh session will hang while I am running that restart script. Is there a way to fake this? Eg. break out of the current ssh session and re-connect to it? –  Carmen Jun 8 '11 at 0:10
1  
Yeah, screen, as noted by today's answer. –  mfinni Jun 11 '11 at 13:40
add comment

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.

echo "sleep 5; /etc/init.d/networking start" | at now
nohup sh -c 'sleep 5; /etc/init.d/networking start' &

Other distributions have the daemon command to turn the resulting program into a daemon that is no longer associated with the shell.

share|improve this answer
add comment

A very simple way to do this is by using the and operator:

service network stop && sleep 5 && service network start
share|improve this answer
1  
I'd prefer service network stop; sleep 5; service network start. If any of the commands fail it will execute the rest anyway. –  ghm1014 Jun 9 '11 at 17:30
    
@ghm1014 Aye, that is also a very good alternate form. In fact, that is what I typically use. –  Jordan S. Jones Jun 9 '11 at 18:42
    
What is the advantage over service network restart? –  Caleb Jun 11 '11 at 14:25
add comment

Try this (maybe installing cron if needed):

$ at now+5min
at> service network stop
at> sleep 5
at> service network start
at> [control-D]

Then logout, wait 6 min and relogin

share|improve this answer
1  
The at scheduler is not in the cron package on most distros, try looking for an at package directly. –  Caleb Jun 11 '11 at 14:23
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.