Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Suspend system on inactivity
# /etc/systemd/system/suspend-on-idle.service
# call "systemctl daemon-reload" after creation / modification
# call "systemctl status suspend-on-idle.service" to see last log line
# call "journalctl -b0 -u suspend-on-idle" to see all log line
[Unit]
Description=Suspend on idle
Wants=network-online.target
After=network.target network-online.target
[Service]
Type=simple
ExecStart=/usr/local/sbin/suspend-on-idle 15
#!/bin/bash
##
# Usage: suspend-on-idle TIMEOUT
#
# Checks if the system has been idling (user logins) for at
# least TIMEOUT minutes.
#
# On Login (including SFTP logins) systemd creates a directory
# with the user ID under /var/run/user. Since the login manager
# also starts with a user the check of these values should be
# restricted to UIDs greater than 999 and UID = 0
#
# This information includes active screen sessions.
#
# Also, the mtime of this directory changes when a
# user quits (and thus the entry is removed). This
# allows us to monitor short-time logins between our
# own checks.
#
# When smbstatus is available the script checks for
# active SMB sessions.
#
# When all checks have passed the script calls "systemctl suspend".
#
# Changes:
# 2017-03-04 Roland Tapken: Parse 'loginctl' instead of checking /var/run/users
##
if [ "$1" == "" ]; then
echo "Usage: $0 TIMEOUT_IN_MINUTES" >&1
exit 127
fi
TIMEOUT="$1"
# Check the last time we send the system to sleep
if [ -e /var/run/suspend-on-idle ]; then
if [ "$(find /var/run/suspend-on-idle -maxdepth 0 -mmin "-$TIMEOUT")" != "" ]; then
# Just return
exit
fi
fi
for n in $(loginctl --no-legend list-sessions | tr -s \ | cut -d\ -f2); do
(
eval $(loginctl -p State -p Class -p User show-session $n | grep -P '^\w+=\w+$')
if [ "$Class" == "user" -a "$State" == "active" ]; then
echo "Found active session $n for user $User"
exit 1
fi
) || exit
done
# Check for last user activity
if [ "$(find /var/run/user -maxdepth 0 -mmin "-$TIMEOUT")" != "" ]; then
echo "Found activity in /var/run/user within the last $TIMEOUT minutes."
exit
fi
# Check for active smb connections
if which smbstatus > /dev/null; then
if smbstatus -p | grep -q '^[0-9]'; then
echo "Found active SMB sessions:"
smbstatus -S
exit
fi
fi
# Check for previous connections
# (Removed - false positives)
#if [ -e /var/run/samba/msg.lock ]; then
# if [ "$(find /var/run/samba/msg.lock -mmin "-$TIMEOUT")" != "" ]; then
# echo "Found SMB sessions within the last $TIMEOUT minutes."
# exit
# fi
#fi
# Ok, go to sleep...
touch /var/run/suspend-on-idle
echo Going to sleep
# Ensure Wake-on-LAN is enabled for all devices that support it
if which ethtool > /dev/null 2>&1; then
for iface in /sys/class/net/*; do
iface="$(basename "$iface")"
if ethtool "$iface" | grep -qi "wake.*:.*g"; then
ethtool -s "$iface" wol g
fi
done
fi
systemctl suspend
# /etc/systemd/system/suspend-on-idle.timer
# call "systemctl daemon-reload" after creation / modification
# call "systemctl start suspend-on-idle.timer" to start timer
# call "systemctl enable suspend-on-idle.timer" to enable timer after reboot
[Unit]
Description=Suspend on idle timer
[Timer]
OnBootSec=15min
OnUnitInactiveSec=5min
Unit=suspend-on-idle.service
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.