Tell me more ×
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 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 own shell. How can i do that? Note that the reboot command without -f doesnot 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? – Coffe_Mug 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. :-) – Coffe_Mug Dec 12 '12 at 13:14
ssh host "command to run on the host machine > /dev/null &" – Coffe_Mug Dec 12 '12 at 13:15

6 Answers

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! :-) – Coffe_Mug Dec 13 '12 at 8:50
is there any alternative for this solution? – Coffe_Mug 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. – Coffe_Mug 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 dont exactly 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 i

share|improve this answer

Try this command:

$ reboot -f && exit
share|improve this answer
2  
Unfortunately this doesn't work. – Coffe_Mug 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

Also You can use

shutdown -r now ; exit
share|improve this answer
1  
Useless. This is exactly equivalent to reboot -f. – Gilles Dec 12 '12 at 23:20

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.