Skip to content

Instantly share code, notes, and snippets.

@BenSjoberg
Created January 11, 2017 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenSjoberg/8459c7cbeacedb2de74536866b9256ce to your computer and use it in GitHub Desktop.
Save BenSjoberg/8459c7cbeacedb2de74536866b9256ce to your computer and use it in GitHub Desktop.
Get systemd scheduled shutdown time
#!/bin/bash
# Determines if a shutdown is scheuled and prints the time if so.
#
# Warning: Quick hack! Basically zero error tolerance.
#
# Designed for Ubuntu 16.04. Will probably work on other systemd-based distros,
# but I haven't tested it on anything else.
#
# There is almost certainly a better way to do this, but I got tired of looking.
#
# Example usage:
# $ shutdown -r 2:00
# Shutdown scheduled for Thu 2017-01-12 02:00:00 CST, use 'shutdown -c' to cancel.
# $ ./when_shutdown.sh
# Shutdown of type "reboot" scheduled for 2017-01-12 02:00 CST
# $ shutdown -c
# $ ./when_shutdown.sh
# No shutdown scheduled
set -e
# Get info about the pending shutdown
read shutdown_type time_usec <<<$(\
busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown | \
awk '{print $2 " " $3}' \
)
if [ $time_usec -eq 0 ]
then
echo "No shutdown scheduled"
exit
fi
time_formatted=$(date -d @$(($time_usec/1000000)) '+%Y-%m-%d %R %Z')
echo "Shutdown of type $shutdown_type scheduled for $time_formatted"
@demonbane
Copy link

demonbane commented May 23, 2017

@BenSjoberg Here's an (almost) pure awk version:

#!/usr/bin/awk -f
BEGIN {
        "busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown" | getline result
        split(result,results)
        results[3]/=1000000
        if (results[3] != 0)
                print "Shutdown of type " results[2] " scheduled for " strftime("%Y-%m-%d %R %Z",results[3],0)
}

@demonbane
Copy link

And here's a way of overloading the shutdown command with a -w option to basically do the above:

https://github.com/demonbane/helpers/blob/c9ffca491f412e68d9407111d44a6aec7e5fe2bc/bash-functions#L82-L99

(a few changes in output so it matches the output used by shutdown -h <time>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment