Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am using the reboot -f command remotely to force reboot a Unix machine. The problem is that the ssh connection remains active for a long time which I don't know why? I want to close the ssh connection immediately after rebooting the machine and return to my local shell. How can i do that? Note that the reboot command without -f flag does not work.

share|improve this question
1  
Why not just exit your remote connection (Ctrl+D) and let the server reboot without you having to watch the shell prompt? –  gertvdijk Dec 12 '12 at 9:35
    
How can i do this in the same command? –  coffeMug Dec 12 '12 at 10:23
1  
I found a solution for this which might be helpful for others as well. I used the following command to close the connection right after starting the command attached to ssh: ssh host "command to run on the host machine > /dev/null &" I don't fully understand the reason why this command forces the connection to close done but at least it was helpful for me. If anyone understands the directing of the output of the command to /dev/null and why it kills the ssh connection it would be nice if he/she can explain it. :-) –  coffeMug Dec 12 '12 at 13:14
    
ssh host "command to run on the host machine > /dev/null &" –  coffeMug Dec 12 '12 at 13:15

5 Answers 5

up vote 2 down vote accepted

The command reboot -f never returns (unless you didn't have permission to cause a reboot). At the point where it is issued, the SSH client is waiting for something to do, which could be:

  • the SSH server notifying the client that something happened that requires its attention, for example that there is some output to display, or that the remote command has finished;
  • some event on the client side, such as a signal to relay;
  • a timer firing up to cause the client to send a keepalive message (and close the connection if the server doesn't reply).

Since the SSH server process is dead, the SSH client won't die until the timer fires up.

If you run ssh remotehost 'reboot -f >/dev/null &', then what happens is:

  1. The remote shell launches the reboot command in the background.
  2. Because the server-side shell command has exited and there is no process holding the file descriptor for standard output open, the SSH server closes the connection.
  3. The reboot command causes the machine to reboot.

However, this is not reliable: depending on timing, step 3 might happen before step 2. Adding a timer makes this unlikely:

ssh remotehost '{ sleep 1; reboot -f; } >/dev/null &'

To be absolutely sure that the server side is committed to running reboot, while making sure that it doesn't actually reboot before notifying the client that it is committed, you need an additional notification to go from the server to the client. This can be output through the SSH connection, but it gets complicated.

share|improve this answer
    
Thanks Gilles for the complete explanation! :-) –  coffeMug Dec 13 '12 at 8:50
    
is there any alternative for this solution? –  coffeMug Dec 13 '12 at 9:16

How about exit from ssh session and reboot system using next command:

ssh login@host "reboot -f"

After this just press Ctrl+C for terminate ssh.

share|improve this answer

Have you tried the following

# shutdown -r now

I find that on some systems I worked on in the pass, the reboot command had some problems. Then again I can't find anything in the manpage of shutdown that would do the same as reboot with the -f flag.

share|improve this answer
1  
Yes, shutdown doesnot work in the machine which i am trying to connect to for some strange reasons. That is why i use reboot -f to force a shutdown and restart. –  coffeMug Dec 12 '12 at 13:08

I found a solution for this which might be helpful for others as well. I used the following command to close the connection right after starting the command attached to ssh:

ssh host "command to run on the host machine > /dev/null &"

I don’t exactly understand the reason why this command forces the connection to close but at least it was helpful for me. If anyone understands the logic behind it and why it kills the ssh connection; it would be nice if he/she explains it.

share|improve this answer

Try this command:

$ reboot -f && exit
share|improve this answer
2  
Unfortunately this doesn't work. –  coffeMug Dec 12 '12 at 10:27
1  
@AKh_Sw Be specific to "doesn't work". –  gertvdijk Dec 12 '12 at 12:44
    
@AKh_Sw : if you typed the '$' sign it won't work –  tH0r Dec 12 '12 at 13:09
1  
Useless. This is exactly equivalent to reboot -f. –  Gilles Dec 12 '12 at 23:21

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.