You are not logged in.
I'd like to have a bash script get triggered automatically before the system goes into suspend/sleep. The script is question is managed by system via a user service unit and it is my understanding that systemd lacks this ability when using user units.
Some thoughts (please share your own and critique these):
* One solution could be to use a systemd sleep hook but that requires a system unit and in my case, that would mix a system unit with a user unit which is complex and just seems wrong.
* Another solution could be to have the bash script some how listen to dbus and execute a function when dbus tells it that the system is going to suspend. I'm thinking that could be useful if I could also employ a systemd-inhibit command that could be cleared after the script's function I want to call on suspend completes.
What do knowledgeable folks think about these option? Better ideas are welcome.
Last edited by graysky (2020-04-18 16:22:06)
Offline
How exactly does it lack abilty?
systemctl --user edit --full --force TestSuspend# /home/koffeinfriedhof/.config/systemd/user/TestSuspend.service
[Unit]
Description=Before sleep
Before=sleep.target
[Service]
Type=oneshot
ExecStart=/usr/bin/logger "System going to sleep" $1
[Install]
WantedBy=sleep.targetsystemctl --user enable --now TestSuspend
systemctl suspendwill trigger the message before sleeping.
Offline
@koffeinfriedhof - I must be complicating the situation. My setup is actually 3 units... perhaps there is an easier way to configure it.
The expected behavior is to have systemd in user mode pass 3 different augments to the script under 4 different circumstances:
1. Pass "start" when the service is started
2. Pass "stop" when the service is stopped
3. Pass "resync" once per hour
4. Pass "resync" if the system is suspended
To do 1 and 2 I have psd.service
To do 3 I created a separate service/timer and joined them to the first one, psd-resync.timer and psd-resync.service
These do not cover #4. I tried adding the sleep.target to psd-resync.service but it is not honored when the system is suspended. I tried creating a 4th service (psd-resync-on-suspend.service) that looks very much like your example, and i edited psd.service adding a Wants=psd-resync-on-suspend.service line, but that lead to two calls of "resync" which is not right... that new service ONLY be triggered on a system suspend. Do you have any thoughts?
Offline
and i edited psd.service adding a Wants=psd-resync-on-suspend.service line
Why did you do that?
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
@Trilby - Thinking that would wed it to psd.service which is all the user should need to call (psd-resync is automatic so this should be as well).
Offline
Ha... I just replicated the steps in post #2 and they are not really correct.
The log message is produced ... when the service is enabled (because it is also started due to the --now flag) but it does not make another log entry when the system suspends. In other words, the service file in post #2 does NOT activate on suspend (at least not without some other service to bridge system <-> user suspend state).
Last edited by Trilby (2020-04-18 22:57:09)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
OK ... back to original question:
I'd like to have a bash script get triggered automatically before the system goes into suspend/sleep. The script is question is managed by system via a user service unit and it is my understanding that systemd lacks this ability when using user units.
Some thoughts (please share your own and critique these):
* One solution could be to use a systemd sleep hook but that requires a system unit and in my case, that would mix a system unit with a user unit which is complex and just seems wrong.* Another solution could be to have the bash script some how listen to dbus and execute a function when dbus tells it that the system is going to suspend. I'm thinking that could be useful if I could also employ a systemd-inhibit command that could be cleared after the script's function I want to call on suspend completes.
What do knowledgeable folks think about these option? Better ideas are welcome.
Offline
I've confused myself with this unit-test, BIG SORRY and thanks for finding the logical error.
Offline
I've confused myself with this unit-test, BIG SORRY and thanks for finding the logical error.
No problem, thank you for contributing.
Offline
I recently wanted to address the same issue and came up with this. The example config shows how to trigger a command on suspend and/or resume.
Offline
I recently wanted to address the same issue and came up with this. The example config shows how to trigger a command on suspend and/or resume.
Thanks for the link.
Offline
https://github.com/systemd/systemd/issues/15477
It seems this is the way to go:
* Another solution could be to have the bash script some how listen to dbus and execute a function when dbus tells it that the system is going to suspend. I'm thinking that could be useful if I could also employ a systemd-inhibit command that could be cleared after the script's function I want to call on suspend completes.
Offline
How about an alternative approach using a system service that runs the sync thing for all logged in (and/or active) users?
(Regardless of the cause, timer or suspend)
Offline
Hi graysky. I have been using the below systemd user units for quite a while now. Perhaps this can get you the desired effect. I only integrated a recent cosmetic psd commit. My user's UID is 1001, so I needed that too in the psd-sleep.service.
$ cat /etc/systemd/user/psd.service
[Unit]
Description=Profile-sync-daemon
Documentation=man:psd(1) man:profile-sync-daemon(1)
Documentation=https://wiki.archlinux.org/index.php/Profile-sync-daemon
Wants=psd-resync.service
RequiresMountsFor=/home/
After=winbindd.service
[Service]
Type=oneshot
RemainAfterExit=yes
# psd-resync.service will do the (re)sync due to Wants=psd-resync.service
ExecStop=/usr/bin/profile-sync-daemon unsync
[Install]
WantedBy=default.target
# suspend/resume support
Also=psd-sleep.service
$ cat /etc/systemd/user/psd-sleep.service
[Unit]
Description=Profile-sync-daemon sleep
Documentation=man:psd(1) man:profile-sync-daemon(1)
Documentation=https://wiki.archlinux.org/index.php/Profile-sync-daemon
Before=sleep.target
StopWhenUnneeded=yes
[Service]
Type=oneshot
RemainAfterExit=yes
Environment=XDG_RUNTIME_DIR=/run/user/1001
ExecStart=/usr/bin/systemctl --user stop psd
ExecStop=/usr/bin/systemctl --user start psd
[Install]
WantedBy=sleep.targetOffline
Brain fart, scratch my previous message. My apologies for the noise.
Offline