search

Suspend Resume Hooks

Wed, Jun 24, 2020 10:15 CEST

Tags: Automation, Screen Lock, Suspend, systemd

For instance, to lock the screen on suspend. Can also be used to toggle WiFi, Bluetooth, etc.

Define the two following units:

A unit for when the system goes to sleep.

# /etc/systemd/system/suspend@.service
[Unit]
Description=Suspend actions for user %I
Before=sleep.target

[Service]
User=%i
Type=forking
Environment=DISPLAY=:0
ExecStart=/usr/bin/env sh /home/%i/.bin/on-suspend
ExecStartPost=/usr/bin/sleep 1

[Install]
WantedBy=sleep.target

And a unit for when the system resumes from sleep.

# /etc/systemd/system/resume@.service
[Unit]
Description=Resume actions for user %I
After=suspend.target

[Service]
User=%i
Type=simple
ExecStart=/usr/bin/env sh /home/%i/.bin/on-resume

[Install]
WantedBy=suspend.target

Enable suspend@username and resume@username.

Edit the scripts on-suspend and on-resume.

See also