How do I reboot over a ssh connection without a return code of -1?
Solution 1
Use the shutdown
command.
shutdown --reboot +1 "System is going down for reboot in 1 minute"
I suspect the reason reboot
doesn't work is because it requires a tty. You could try running it with a background tty, but the shutdown
command has everything you need, including cancelling -- as it says in response:
Shutdown scheduled for Thu 2018-02-22 15:19:33 MST, use 'shutdown -c' to cancel.
Solution 2
Usually you can return true
and that will return an exit code of 0 so:
shutdown -r now || true
Related videos on Youtube
YetAnotherRCG
Updated on September 18, 2022Comments
-
YetAnotherRCG almost 2 years
The system I am working with uses
ssh
to remotely connect to a Linux machine. It then executes a single shell command and analyses the output from the shell command.If I run
reboot
, I get exit code-1
, since rebooting of course kills the ssh connection. Any exit code other than 0 makes the system register a failure, thus I have been trying to write a single line command that will reboot and exit thessh
session gracefully.The machines in question are very bare bones and the reboot utility does not allow any options so I can't just schedule a reboot for later.
After some thought I tried running
$ sleep 3 && reboot & exit
Which works when I call it manually: the connection closes with error code
0
and 3 seconds later the machine reboots. Great.But the same command run through our system doesn't actually reboot. It just returns exit code 0 and the reboot never happens.
Why would this be?
-
Admin over 6 yearsI'm not certain why it's not working, but you could try
ssh user@host "nohup init 6 < /dev/null > /dev/null 2>/dev/null"
. Alternatively,ssh user@host "nohup shutdown -r +1 > /dev/null 2>/dev/null"
which will reboot the system one minute after the command is sent.
-
-
YetAnotherRCG over 6 yearsThat explains it, one minute is a little long to wait but it does work thanks.
-
Rich over 6 yearsSay
now
instead of+1
and change the message to suit. There isn't a way to specify a shorter interval (e.g.+15s
would mean "in fifteen seconds"), but you can specify the next minute, so if it's 10:57 now, you could say10:58
as your time specification. -
Admin almost 2 yearsUnfortunately, this didn't work for me.