You are not logged in.

#1 2020-04-16 11:11:43

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,631
Website

Systemd service needs to trigger by a timer and on suspend

I'd like to have a service to run under two conditions:
1) When a timer tells it to run
2) When the system is suspended

I have condition 1 working now, the service does not get triggered on condition 2.  Thoughts are welcomed.

psd-resync.service
psd-resync.timer

Last edited by graysky (2020-04-16 13:23:16)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2020-04-16 12:41:58

Ropid
Member
Registered: 2015-03-09
Posts: 1,069

Re: Systemd service needs to trigger by a timer and on suspend

I have a service that I wanted to run at boot and after suspend, and I didn't manage to put both rules into the same service file, my experiments failed. I ended up copying the service file and using two files: one for boot and one for suspend.

I've seen someone use a service file for suspend where the ExecStart= line runs "systemctl restart ...", so that you can avoid having to keep the ExecStart lines in the two files synchronized.

Offline

#3 2020-04-16 14:09:41

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,631
Website

Re: Systemd service needs to trigger by a timer and on suspend

I don't think a unit calling systemctl is right... perhaps I am expecting the sleep.target to work in user mode?  Maybe someone more knowledgeable than I can comment given some time.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#4 2020-04-16 14:19:16

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: Systemd service needs to trigger by a timer and on suspend

You are correct in that sleep.target will not work from user mode. User units (oddly) can't depend on system services. You can run the unit as a system service and use the `User=` directive.

Offline

#5 2020-04-16 14:33:57

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Systemd service needs to trigger by a timer and on suspend

firecat53 is right about that

Separate files should be created for user actions and root/system actions. Enable the suspend@user and resume@user services to have them started at boot

(see) https://wiki.archlinux.org/index.php/Po … leep_hooks

Offline

#6 2020-04-16 14:37:45

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,152
Website

Re: Systemd service needs to trigger by a timer and on suspend

To add one other thought, I highly doubt having a timer also trigger a service would interfere at all with suspend/sleep triggers.  So the fact that you want each of these to trigger it may be tangential to the issue.  It does seem that people often struggle getting services to run on suspend/sleep, but focus on that.  Once that's done properly, then just add the timer.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2020-04-16 16:07:51

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,631
Website

Re: Systemd service needs to trigger by a timer and on suspend

It seems like the sleep hook option qinohe posted might be appropriate but enabling a separate service external to the my script seems odd.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#8 2020-04-16 17:38:16

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Systemd service needs to trigger by a timer and on suspend

I don't think you have that many options beside scripting it in your psd service/app itself which ads way more complexity(test for suspend/hibernate and act to that) to psd than using the extra sytemd service, I could be wrong of course but that seem to be your limitations..

I have a timer that starts a script which counts back and stops if I reboot/poweroff for a PI which uses the complex method and it needs more files/scripts than a simple extra service...

Offline

#9 2020-04-17 05:45:46

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,631
Website

Re: Systemd service needs to trigger by a timer and on suspend

What do people think about:

1. A system-level Sleep_hook suggested by qinohe.  Since I am using a user service, this would seem to be mixing system and user mode units which just seems messy and wrong to me.

2. Some code that hooks into systemd-logind's inhibitors.  It would need to be triggered by psd itself which I currently do not do nor have any idea how to do.

Last edited by graysky (2020-04-18 13:07:54)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#10 2020-04-17 13:18:34

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Systemd service needs to trigger by a timer and on suspend

I saw the question on github though I paste it here;)
Haven't test it, my system is the always on type...
How about this simple hook

##/etc/systemd/system/psd-sleep@.service
[Unit]
Description=Profile-sync-daemon sleep hook
Before=sleep.target
StopWhenUnneeded=yes

[Service]
User=%I
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/profile-sync-daemon resync
ExecStop=/usr/bin/profile-sync-daemon unsync

[Install]
WantedBy=sleep.target

PS. You second option is quite extreme for a service like Psd wink
I would almost say leave it to the user, like Trilby said getting the service to act/behave nice on sleep/suspend would be the main concern...

Offline

#11 2020-04-17 13:31:47

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,631
Website

Re: Systemd service needs to trigger by a timer and on suspend

@qinhoe - Thanks for the suggestion.  I wonder if one can mix user units with system units with a PartOf= as I have needed to do with the start/stop and hourly resync.  Where on github did you find the suggestion?

Last edited by graysky (2020-04-17 13:33:27)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#12 2020-04-17 13:42:27

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Systemd service needs to trigger by a timer and on suspend

I didn't find the answer on github, I saw the question of a user (mattia-b89) https://github.com/graysky2/profile-syn … issues/231
About the fact if it's usable with the current way psd works you need to test,
First try without the timer as Trilby suggested, than later add the timer.

Offline

#13 2020-04-18 13:25:00

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,631
Website

Re: Systemd service needs to trigger by a timer and on suspend

If I cannot find a solution in systemd itself (short of mixing user and system units, I cannot), from that stackexchange post, it seems I need to listen for dbus to see when the suspend event is triggered, and run my script prior.  Need to google a bit but if anyone following this post has some experience/suggestion, I would be grateful if you share them.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#14 2020-04-18 13:29:12

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,152
Website

Re: Systemd service needs to trigger by a timer and on suspend

You may want to adjust the thread title.  It seems the main question is how to have a user service trigger by suspend.  The timer is a red-herring.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#15 2020-04-18 13:33:26

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,631
Website

Re: Systemd service needs to trigger by a timer and on suspend

Trilby wrote:

You may want to adjust the thread title.  It seems the main question is how to have a user service trigger by suspend.  The timer is a red-herring.

You're right, I think a new thread might be less confusing rather then editing.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

Board footer

Powered by FluxBB