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
& disown are the most suitable ones:
setsid bash -c 'sleep 10 && reboot' &
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' &"
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.
Thanks to @UlrichSchwarz I came across this trick:
echo 'sleep 30; reboot' | at now
And this also works well with
ssh user@server -t "echo 'sleep 30; reboot' | at now"
at doing differently from