0

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?

miu
  • 265
  • 2
    I think you're really looking for the at daemon, if you can live with 1-minute resolution: echo reboot | at now + 1 minutes – Ulrich Schwarz Oct 15 '20 at 05:12
  • @UlrichSchwarz You're right, that's what I was looking for and yes, minute resolution is fine for me. Thanks a lot! (But the at command wasn't preinstalled.) – miu Oct 15 '20 at 05:28
  • But maybe someone is able to explain why my previous commands didn't work. – miu Oct 15 '20 at 05:30
  • 1
    A related question is https://unix.stackexchange.com/q/465322/5132 . – JdeBP Oct 15 '20 at 06:29

1 Answers1

0

I think your requirements would be served by the shutdown command which is capable of scheduling its activity to a later date if required. For instance using the "-r" switch for reboot and "+1" for a delay of 1 minute. However, shutdown does require elevation with sudo.

sudo shutdown -r '+1'

you can also do this for immediate reboots too using "now" rather than "+1".

sudo shutdown -r now
MNB
  • 131
  • 4