Skip to content

Instantly share code, notes, and snippets.

@tskrynnyk
Created October 13, 2016 01:11
Show Gist options
  • Select an option

  • Save tskrynnyk/1dea21f356101cd98cef0f0489b694ed to your computer and use it in GitHub Desktop.

Select an option

Save tskrynnyk/1dea21f356101cd98cef0f0489b694ed to your computer and use it in GitHub Desktop.
systemd: hibernate after suspend
# /etc/systemd/system/hibernate-after-suspend.service
[Unit]
Description=Hibernate after suspend
Documentation=https://bbs.archlinux.org/viewtopic.php?pid=1420279#p1420279
Documentation=https://bbs.archlinux.org/viewtopic.php?pid=1574125#p1574125
Documentation=https://wiki.archlinux.org/index.php/Power_management
Documentation=https://wiki.debian.org/SystemdSuspendSedation
Conflicts=hibernate.target hybrid-suspend.target
Before=suspend.target
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=yes
Environment="ALARM_SEC=10800"
Environment="WAKEALARM=/sys/class/rtc/rtc0/wakealarm"
ExecStart=-/usr/sbin/rtcwake --seconds $ALARM_SEC --auto --mode no
ExecStop=-/bin/sh -c '\
ALARM=$(cat $WAKEALARM); \
NOW=$(date +%%s); \
if [ -z "$ALARM" ] || [ "$NOW" -ge "$ALARM" ]; then \
echo "hibernate-after-suspend: Woke up - no alarm set. Hibernating..."; \
systemctl hibernate; \
else \
echo "hibernate-after-suspend: Woke up before alarm - normal wakeup."; \
/usr/sbin/rtcwake --auto --mode disable; \
fi \
'
[Install]
WantedBy=sleep.target
RequiredBy=suspend.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment