I'm trying to reboot the server after a specified time.
If logged in on the server this can be achieved with a command like this:
sleep 10 && reboot
To make this command stay alive (without tmux or screen), even if the shell terminates, I tried many different options, but setsid
and & disown
are the most suitable ones:
setsid bash -c 'sleep 10 && reboot' &
... or:
bash -c 'sleep 10 && reboot' & disown
This is working so far on the server, even if I close the shell where I called this command from. But since I want to restart the server through ssh and quit immediately after calling that command (without waiting for the specified sleep
time period), I assumed to be able to achieve that with the adjusted commands from above, like so:
ssh user@server -t "setsid bash -c 'sleep 10 && reboot' &"
... or:
ssh user@server -t "bash -c 'sleep 10 && reboot' & disown"
But interestingly these commands launch a process with the name (sd-pam)
which will exit after the specified sleep
time period without doing anything. But if you execute the command often enough, you will find that it does it's job in about 10% of cases. I investigated that behavior and found out, that the (sd-pam)
process is limited in many ways (e.g. not able to read and write to and from files - therefore I cannot log anything in case the reboot command is throwing an error).
All commands were executed with the root
user on Fedora 32.
Edit:
Thanks to @UlrichSchwarz I came across this trick:
echo 'sleep 30; reboot' | at now
And this also works well with ssh
:
ssh user@server -t "echo 'sleep 30; reboot' | at now"
What is at
doing differently from setsid
and & disown
?
at
daemon, if you can live with 1-minute resolution:echo reboot | at now + 1 minutes
– Ulrich Schwarz Oct 15 '20 at 05:12at
command wasn't preinstalled.) – miu Oct 15 '20 at 05:28