26

I just received notification that our site has a power outage tomorrow morning.

I am a Windows admin but I have to cover for our Linux admin who's not around until tomorrow evening.

I need to shutdown our RHEL server at 06:45 tomorrow morning (without me doing it).

I have searched on here but see mixed answers using shutdown, some say -h, some say -p, some say something completely different.

It's ~21:15 now and I need to shutdown at 06:45 in the morning. What is the simplest way I can schedule this?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • shutdown command have an option to start in futur. use man shutdown on your system. – Archemar Nov 26 '14 at 21:24
  • 1
    I think there may have been more attempts search as this question is found in many forums, http://unix.stackexchange.com/questions/120506/how-to-shutdown-linux-at-a-specific-datetime-from-terminal/120509#120509 – tachomi Nov 26 '14 at 21:57

3 Answers3

37

You should use the at command:

$ sudo at 6:45
[sudo] password for root: 
warning: commands will be executed using /bin/sh
at> poweroff
at> <EOT>

Don't type the <EOT>, but press Ctrl+D at the second at> prompt.

The significant advantage of using at over using shutdown with a TIME argument, is that it involves real, persistent, scheduling, and works even if the machine is rebooted in the intermediate time period. The shutdown TIME will not restart automatically in such an event, which might cause a double ungraceful power off if the reboot in the intermediate time period was not anticipated.

Anthon
  • 79,293
21

You can use shutdown:

sudo shutdown -h  06:45 &

And to check it:

ps -aux | grep shutdown

If you want to cancel it:

sudo shutdown -c

This assumes of course that the shutdown time has already passed.

jmunsch
  • 4,346
  • Thanks for the answer but when I do this it just hangs until I Ctrl+C it ... `]# shutdown -g 06:45

    Shutdown cancelled. `.

    – user4166144 Nov 26 '14 at 21:26
  • 1
    @user4166144 Updated to background the process. – jmunsch Nov 26 '14 at 21:28
  • Great. I have shutdown -g 06:45 in ps output now. I take it I'm ok to close the SSH session, right? – user4166144 Nov 26 '14 at 21:33
  • If it said that it is going to shut down in XXX minutes. It should shutdown within a few seconds of that time. – jmunsch Nov 26 '14 at 21:36
  • What exactly is the -g option? My Fedora 21 and CentOS 7 systems don't have that option. They just need the time, as in shutdown 06:45. – garethTheRed Nov 26 '14 at 21:44
  • I think its short for graceperiod. It will after the graceperiod run shutdown -h time Although it might have been deprecated. -h would work just as well. And is documented. I updated my answer. – jmunsch Nov 26 '14 at 21:52
  • 10
    instead of killall, why not shutdown -c ? –  Nov 26 '14 at 23:16
  • 1
    About closing the ssh session --- you can check (close and enter again, check if the shutdown is still running). Otherwise you'll need sudo nohup shutdown... ---although it shouldn't be needed (the HUP signal will be sent as a normal user, so...) – Rmano Nov 27 '14 at 19:23
0

Another way

su -c 'echo "systemctl poweroff" | at 06:45'

zorbax
  • 340
  • 4
    Assumption in your comment is RHEL7. Earlier versions would need to be done differently. – mdpc Nov 26 '14 at 22:38