shutdown: add time argument, wall message, and cancel

shutdown ignored everything after its options, so a delayed shutdown
went down right away:

    shutdown -r +10 "Upgrading, back in five"

Take a time, now, +MIN, or the next HH:MM, and an optional message.
Finit keeps the countdown, so it survives logging out:

  - warns logged-in users at 60, 30, 15 and 10 minutes left, then
    every minute from five, and on the console
  - writes the message to /etc/nologin from five minutes before,
    leaving one already in place alone
  - shutdown -c cancels, and a new shutdown TIME replaces it

Without a time, or with now and no message, shutdown acts right away
as before.  An immediate reboot, halt, or power-off, also over D-Bus,
cancels a scheduled one.  The warnings are written by a child, reading
utmp and closing a serial tty can block.

Fixes #149

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-17 11:00:49 +01:00
1 parent d51f521bf8
commit 20c422af69
12 files changed
+542 -53

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
#!/bin/sh
# Verify scheduled shutdown, see issue #149: shutdown TIME [MESSAGE]
# blocks new logins from five minutes before, and shutdown -c cancels
# it. The countdown never runs out here, it is always cancelled.
set -eu
TEST_DIR=$(dirname "$0")
NOLOGIN=/etc/nologin
# Assert that the command, run in the test namespace, fails
fails()
{
assert "$1 fails" "$(texec sh -c "$1 >/dev/null 2>&1 || echo fail")" = fail
}
test_teardown()
{
say "Running test teardown."
run "shutdown -c 2>/dev/null || true"
run "rm -f $NOLOGIN"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
sep
say 'Schedule a reboot in two minutes, within the nologin window'
run "shutdown -r +2 Testing scheduled reboot"
retry "assert_file_contains $NOLOGIN 'Testing scheduled reboot'"
say 'Cancel it, nologin is removed and the system stays up'
run "shutdown -c"
retry "texec sh -c '! test -e $NOLOGIN'"
assert "Still in runlevel 2" "$(texec initctl runlevel | awk '{print $2}')" = 2
sep
say 'Nothing to cancel'
fails "shutdown -c"
say 'An invalid time is refused'
fails "shutdown -r 25:00"
fails "shutdown -r +999999999"
sep
say 'A later shutdown replaces it, and its nologin'
run "shutdown -r +2 First"
retry "assert_file_contains $NOLOGIN First"
run "shutdown -r +120 Second"
retry "texec sh -c '! test -e $NOLOGIN'"
run "shutdown -c"
sep
say 'A nologin file of the admin is left alone'
run "echo 'Down for maintenance' > $NOLOGIN"
run "shutdown -h +2"
sleep 1 # first tick, where nologin would be written
run "shutdown -c"
assert_file_contains $NOLOGIN 'Down for maintenance'