From edecb56216c910b36980fcf590b385ede413392b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 15 May 2025 14:58:07 +0200 Subject: [PATCH] man: document how to hook stuff into system wakeup Fixes: #6364 --- man/systemd.special.xml | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/man/systemd.special.xml b/man/systemd.special.xml index 0195bcb7fb..3e5e90cae8 100644 --- a/man/systemd.special.xml +++ b/man/systemd.special.xml @@ -754,12 +754,38 @@ sleep.target - A special target unit that is pulled in by - suspend.target, - hibernate.target, - suspend-then-hibernate.target, and - hybrid-sleep.target and may be used to - hook units into the sleep state logic. + A special target unit that is pulled in by suspend.target, + hibernate.target, suspend-then-hibernate.target, and + hybrid-sleep.target and may be used to hook units into the sleep state + logic. + + In order to hook external programs before the actual system sleep + operation, place their command line in a service unit file's ExecStart= line + (use Type=oneshot), and ensure the unit is pulled in by + sleep.target and ordered before it. In order to hook program code + after the actual system sleep operation (i.e. to be run after the system + woke up again), place the command in ExecStop= instead, and make sure to + enable StopWhenUnneeded= and RemainAfterExit=. Both + approaches can be combined into one unit file in order to run programs both before and after the + sleep operation. + + + Combined Example + + [Unit] +DefaultDependencies=no +StopWhenUnneeded=yes +Before=sleep.target + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/bin/some-before-command +ExecStop=/Usr/bin/some-after-command + +[Install] +WantedBy=sleep.target +