shutdown at specific date

The at command is for scheduling one off future executions.

e.g.

% at 8pm Aug 31
at> echo hello
at> <EOT>
job 161 at Sat Aug 31 20:00:00 2019

(the "<EOT>" was produced by pressing control-D)

% atq
161     Sat Aug 31 20:00:00 2019 a sweh

You can put your shutdown command here.


While at is the obvious way to do this, I think it will survive shutdowns, so if the machine reboots before the scheduled shutdown, it might shutdown again at the scheduled time. Therefore, using bash and GNU date we can use a delayed shutdown.

/sbin/shutdown +$(( ( $( date -d "30 may 2019 13:15" +%s ) - $( date +%s ) ) / 60 + 1 ))

NB: This is only accurate to one minute. It works by converting the scheduled time, and now to seconds since the UNIX epoch, calculating the difference, converting to minutes, and then using this as the delay to shutdown. It won't work correctly if the scheduled time is in the past.


Recent Ubuntu versions use systemd and when the conventional atd and associated at scheduler for one-off commands is either not installed or not running a one-off command can be scheduled with systemd-run which is somewhat easier than manually generating a systemd timer:

 systemd-run --on-calendar="2019-08-31 20:00:00 CET" /sbin/shutdown now