You are not logged in.

#1 2022-02-12 06:05:51

LithoUser
Member
Registered: 2016-11-17
Posts: 173

[SOLVED] Systemd user service timer

Hello!

I've created a systemd user service, and I'd like to create a timer for it; but the wiki page about systemd timers doesn't give examples for user services, only for system wide services (I think).

My service is ~/.config/systemd/user/foo@.service

[Unit]
Description=Firefox profile in memory

[Install]
WantedBy=default.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/firefox-sync %i
ExecStop=/usr/local/bin/firefox-sync %i

I enable it for each of my firefox profiles like this:

systemctl --user daemon-reload
systemctl --user enable firefox-profile@my_first_profile.service
systemctl --user enable firefox-profile@my_second_profile.service

This timer launches the script for each profile each time the user logs in or out; what I'd like is to launch it when the user logs in and out AND every one hour.

Is it possible? Thanks in advance for any help!

Last edited by LithoUser (2022-02-14 09:51:43)

Offline

#2 2022-02-12 07:20:06

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,087

Offline

#3 2022-02-12 07:44:17

LithoUser
Member
Registered: 2016-11-17
Posts: 173

Re: [SOLVED] Systemd user service timer

I'm sorry, but I don't understand the link between my question and temporary files. Could you please explain a little more? Thank you very much!

Offline

#4 2022-02-12 08:01:44

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,087

Re: [SOLVED] Systemd user service timer

It illustrates a user service and timer being enabled?
The specific function of those is irrelevant to your question.

Offline

#5 2022-02-12 08:18:18

LithoUser
Member
Registered: 2016-11-17
Posts: 173

Re: [SOLVED] Systemd user service timer

OK, that's true.

So if I understand correctly, I should try to mimic the behavior of /usr/lib/systemd/system/systemd-tmpfiles-clean.service and systemd-tmpfiles-clean.timer, am I right? But there's no reference in those files to how I could have a variable passed to the script (with the "%i" variable to pass the profile name, like in my service: "ExecStart=/usr/local/bin/firefox-sync %i") and I don't see how I could enable those services for each profile (like I currently do: systemctl --user enable firefox-profile@my_first_profile.service).

Furthermore, the timer seems to trigger the script at a regular time, but not when the user logs in and out:

[Timer]
OnBootSec=15min
OnUnitActiveSec=1d

I guess there's still a lot that I don't get...

Last edited by LithoUser (2022-02-12 11:41:37)

Offline

#6 2022-02-12 21:40:16

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,087

Re: [SOLVED] Systemd user service timer

man systemd.timer wrote:

Unit=

The unit to activate when this timer elapses. The argument is a unit name, whose suffix is not ".timer". If not specified, this value defaults to a service that has the same name as the timer unit, except for the suffix. (See above.) It is recommended that the unit name that is activated and the unit name of the timer unit are named identically, except for the suffix.

Also see the first paragraph in https://man.archlinux.org/man/systemd.t … ESCRIPTION don't overthink this and just have an firefox-profile@.timer and enable that.

Offline

#7 2022-02-13 07:03:28

LithoUser
Member
Registered: 2016-11-17
Posts: 173

Re: [SOLVED] Systemd user service timer

OK, so here is my .config/systemd/user/firefox-profile@.service file:

[Unit]
Description=Firefox profile memory cache

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/firefox-sync %i
ExecStop=/usr/local/bin/firefox-sync %i

And here is my .config/systemd/user/firefox-profile@.timer file:

[Unit]
Description=Activate the firefox-profile service

[Timer]
OnUnitActiveSec=1h

[Install]
WantedBy=timers.target

I've activated it using:

systemctl --user daemon-reload
systemctl --user enable firefox-profile@my_first_profile.timer

But it doesn't work: I've rebooted and logged in, but my first firefox profile isn't synced... Is there a way I can see what has happened? journalctl contains nothing about "firefox-profile".

Last edited by LithoUser (2022-02-13 07:06:52)

Offline

#8 2022-02-13 07:09:26

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] Systemd user service timer

systemctl --user status your.service

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#9 2022-02-13 07:19:19

LithoUser
Member
Registered: 2016-11-17
Posts: 173

Re: [SOLVED] Systemd user service timer

$ systemctl --user status firefox-profile@my_first_profile.timer
 ● firefox-profile@my_first_profile.timer - Activate the firefox-profile service
     Loaded: loaded (/home/me/.config/systemd/user/firefox-profile@.timer; enabled; vendor preset: enabled)
     Active: active (elapsed) since Sun 2022-02-13 08:00:08 CET; 14min ago
      Until: Sun 2022-02-13 08:00:08 CET; 14min ago
    Trigger: n/a
   Triggers: ● firefox-profile@my_first_profile.service

$ systemctl --user status firefox-profile@my_first_profile.service
○ firefox-profile@my_first_profile.service - Firefox profile memory cache
     Loaded: loaded (/home/me/.config/systemd/user/firefox-profile@.service; static)
     Active: inactive (dead)
TriggeredBy: ● firefox-profile@my_first_profile.timer

Last edited by LithoUser (2022-02-13 07:20:39)

Offline

#10 2022-02-13 08:39:09

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,087

Re: [SOLVED] Systemd user service timer

You enabled the timer, but not the service.

what I'd like is to launch it when the user logs in and out AND every one hour.

Sounds like you want both.
You enable the service, what triggers it at login and the timer, what triggers the service every hour.

Offline

#11 2022-02-13 08:57:17

LithoUser
Member
Registered: 2016-11-17
Posts: 173

Re: [SOLVED] Systemd user service timer

Thanks a lot, it seems to work now!

I'd still have two questions:

seth wrote:

You enable the service, what triggers it at login

1. It triggers at login only, or both at login AND logout?

2. I've noticed that if I want to launch Firefox just after I login, the profile has not been synced yet; would there be a way to trigger a little earlier in the login process?

Offline

#12 2022-02-13 09:05:06

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,087

Re: [SOLVED] Systemd user service timer

1. login only, but the service runs firefox-sync when it exits on logout
2. how do you launch firefox and how long does the sync take? You may want to wait out a running firefox-sync process (eg. "while pidof firefox-sync; do sleep 1; done; exec firefox")

Offline

#13 2022-02-13 09:18:14

LithoUser
Member
Registered: 2016-11-17
Posts: 173

Re: [SOLVED] Systemd user service timer

The sync takes about 1 sec; I launch Firefox simply with: firefox -P "my_first_profile" --no-remote

I'll try your suggestion (while... sleep...)

Thank you very much for your help!

Offline

#14 2022-02-13 09:25:33

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] Systemd user service timer

This shouldn't work:

systemd.timer(5) wrote:

Note that in case the unit to activate is already active at the time the timer elapses it is not restarted, but simply left running. There is no concept of spawning new service instances in this case. Due to this, services with RemainAfterExit= set (which stay around continuously even after the service's main process exited) are usually not suitable for activation via repetitive timers, as they will only be activated once, and then stay around forever.

For the hourly runs, you want a more basic oneshot service that runs a single sync command and exits (becomes inactive).

Offline

#15 2022-02-13 16:53:02

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

Re: [SOLVED] Systemd user service timer

seth wrote:

Sounds like you want both.
You enable the service, what triggers it at login and the timer, what triggers the service every hour.

Why enable the service?, your timer can do this:-)

[Unit]
Description=Activate the firefox-profile service

[Timer]
OnBootSec=60min
OnUnitActiveSec=1h
Unit=firefox-profile@%u.service

[Install]
WantedBy=timers.target

Offline

#16 2022-02-14 04:27:41

LithoUser
Member
Registered: 2016-11-17
Posts: 173

Re: [SOLVED] Systemd user service timer

Thanks for your answers Raynman and qinohe!

I've tried to take your comments into account, but now it doesn't work anymore.

From what I understand, I should have:

1. .config/systemd/user/firefox-profile@.service:

[Unit]
Description=Firefox profile memory cache

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/firefox-sync %i
ExecStop=/usr/local/bin/firefox-sync %i

Not sure if that's "a more basic oneshot service that runs a single sync command and exits (becomes inactive)"...

2. .config/systemd/user/firefox-profile@.timer:

[Unit]
Description=Activate the firefox-profile service

[Timer]
OnBootSec=60min
OnUnitActiveSec=1h
Unit=firefox-profile@%i.service

[Install]
WantedBy=timers.target

Since I want to trigger my_first_profile.service, not user.service, that's "%i.service", not "%u.service", right? And shouldn't it be "OnBootSec=0" instead of "OnBootSec=60min", in order to trigger just after login?

Then I enable the timer only:

systemctl --user enable firefox-profile@my_first_profile.timer

I've disabled the (previously enabled) service, rebooted, and... the script is not triggered after login. What am I doing wrong?

Last edited by LithoUser (2022-02-14 04:31:43)

Offline

#17 2022-02-14 05:05:00

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

Re: [SOLVED] Systemd user service timer

Since I want to trigger my_first_profile.service, not user.service, that's "%i.service", not "%u.service", right? And shouldn't it be "OnBootSec=0" instead of "OnBootSec=60min", in order to trigger just after login?

I'm not sure about '%i'
Of course 'OnBootSec=60min' was an example for you to tweak...
You may show us what 'firefox-sync' looks like.
I start a timer which starts a script in a similar way works fine though, "type=simple"
Sometimes, systemd timer > services can be "hard" to configure!

Offline

#18 2022-02-14 05:14:48

LithoUser
Member
Registered: 2016-11-17
Posts: 173

Re: [SOLVED] Systemd user service timer

I'm not sure about '%i'

I think I'm right on this one, since "man systemd.unit" says:

%i For instantiated units this is the string between the first "@" character and the type suffix. Empty for non-instantiated units.

%u This is the name of the user running the service manager instance. In case of the system manager this resolves to "root". Note that this setting is not influenced by the User= setting configurable in the [Service] section of the service unit.

Last edited by LithoUser (2022-02-14 05:15:51)

Offline

#19 2022-02-14 05:21:56

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

Re: [SOLVED] Systemd user service timer

Okay, you may be right about the '%i', yes.

qinohe wrote:

You may show us what 'firefox-sync' looks like.

I gives us a better picture and others can test if they like;-)

Offline

#20 2022-02-14 06:42:49

LithoUser
Member
Registered: 2016-11-17
Posts: 173

Re: [SOLVED] Systemd user service timer

The script is here.

Using "OnBootSec=0" does allow to trigger the script on boot, but "OnUnitActiveSec=1h" doesn't seem to work:

$ systemctl --user list-timers
NEXT  LEFT   LAST                                            PASSED            UNIT                                                      ACTIVATES
n/a      n/a     Mon 2022-02-14 06:25:07 CET   1h 17min ago   firefox-profile@my_first_profile.timer   firefox-profile@my_first_profile.service

Offline

#21 2022-02-14 07:50:46

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,087

Re: [SOLVED] Systemd user service timer

See Raynman's post

Offline

#22 2022-02-14 07:59:31

LithoUser
Member
Registered: 2016-11-17
Posts: 173

Re: [SOLVED] Systemd user service timer

OK, so...

Raynman wrote:

For the hourly runs, you want a more basic oneshot service that runs a single sync command and exits (becomes inactive).

Does "a single sync command" mean that I should delete "ExecStop..." and keep only "ExecStart=..."? But then... my script won't be triggered on shutdown?

a more basic oneshot service [...] and exits (becomes inactive).

I don't know how to do that sad

Offline

#23 2022-02-14 08:22:09

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,292

Re: [SOLVED] Systemd user service timer

[Unit]
Description=Firefox profile memory cache

[Service]
Type=oneshot
RemainAfterExit=false
ExecStart=/usr/local/bin/firefox-sync %i

basically the above, you could also just drop the RemainAfterExit line since false is the default.

Offline

#24 2022-02-14 08:29:31

LithoUser
Member
Registered: 2016-11-17
Posts: 173

Re: [SOLVED] Systemd user service timer

Thanks! It works for login and for the hourly runs, but what about trigger on logout? (not on shutdown, I was wrong to say that before: it's a user service, and I want the script to be triggered on every hour AND each time the user logs in and out)

Offline

#25 2022-02-14 08:53:19

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,292

Re: [SOLVED] Systemd user service timer

Make two services, the original one you keep as you had it at the start and enable it, the second  one you take as this last example and have it triggered by the timer.

Offline

Board footer

Powered by FluxBB