Skip to content

Instantly share code, notes, and snippets.

@atmoz
Last active December 14, 2022 13:48
Show Gist options
  • Save atmoz/17ad127423bb061b033ece035d511ce7 to your computer and use it in GitHub Desktop.
Save atmoz/17ad127423bb061b033ece035d511ce7 to your computer and use it in GitHub Desktop.
Systemd services to hibernate after 2 hours of suspend. This is to increase security and battery usage while preserving a fast resume in working hours. Put files in /etc/systemd/system/
[Unit]
Description=Delayed hibernation trigger
Documentation=https://bbs.archlinux.org/viewtopic.php?pid=1420279#p1420279
Documentation=https://wiki.archlinux.org/index.php/Power_management
Conflicts=hibernate.target hybrid-suspend.target
Before=sleep.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
Environment="WAKEALARM=/sys/class/rtc/rtc0/wakealarm"
Environment="SLEEPLENGTH=+2hour"
ExecStart=-/usr/bin/sh -c 'echo -n "alarm set for "; date +%%s -d$SLEEPLENGTH | tee $WAKEALARM'
ExecStop=-/usr/bin/sh -c '\
alarm=$(cat $WAKEALARM); \
now=$(date +%%s); \
if [ -z "$alarm" ] || [ "$now" -ge "$alarm" ]; then \
echo "hibernate triggered"; \
systemctl hibernate; \
else \
echo "normal wakeup"; \
fi; \
echo 0 > $WAKEALARM; \
'
[Install]
WantedBy=sleep.target 
[Unit]
Description=User suspend actions
Before=sleep.target
[Service]
Type=forking
User=atmoz
Environment=DISPLAY=:0
ExecStart=i3lock
ExecStartPost=/usr/bin/sleep 1
[Install]
WantedBy=sleep.target
# Kill screen locker to avoid typing password two times.
# This only makes sense if you use BIOS password
# or Self-encrypting drive (SED).
[Unit]
Description=Unlock screen after hibernate
After=hibernate.target
[Service]
Type=simple
ExecStart=-/usr/bin/killall i3lock
[Install]
WantedBy=hibernate.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment