Can't shutdown Linux guest OS with `at` command

Here are three examples of what I did inside the terminal
(the third one has sudo):

  1. at now + 5 minutes
    

    (interactively:)

    at> shutdown --poweroff now
    
  2. echo "sbin/shutdown -h now" | at 14:50
    
  3. echo "/sbin/shutdown -h 16:22" | at now
    

In every case,
I can verify the one-time job is scheduled with the atq command,
but the shutdown --show command says "No scheduled shutdown".

The system should shutdown at the scheduled time that at runs
after sleeping.

This is on a CentOS 9 Stream guest virtual machine. 
The user is already added to the sudo users group
and can initiate the shutdown with a command like shutdown now
So, with the one-time at job, what’s the difference?

Asked By: Samir Fink

||

"The user is already added to the sudo users group" – but you don’t use sudo anywhere. You need to apply the administrative privileges somewhere by using the sudo command. For example:

# Use root's "at"
echo /sbin/shutdown -h now | sudo at now +5 minutes

or

# Use your "at" with an embedded "sudo shutdown…"
echo sudo /sbin/shutdown -h now | at now +5 minutes

However, rather than running at it seems that it would seem to make more sense to fire the shutdown command with a specific time and avoid at entirely:

sudo shutdown -h "$(date --date '+5 min' +'%H:%M')"
Answered By: Chris Davies
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.