You are not logged in.
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 %iI 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.serviceThis 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
Online
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
It illustrates a user service and timer being enabled?
The specific function of those is irrelevant to your question.
Online
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=1dI guess there's still a lot that I don't get...
Last edited by LithoUser (2022-02-12 11:41:37)
Offline
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.
Online
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 %iAnd here is my .config/systemd/user/firefox-profile@.timer file:
[Unit]
Description=Activate the firefox-profile service
[Timer]
OnUnitActiveSec=1h
[Install]
WantedBy=timers.targetI've activated it using:
systemctl --user daemon-reload
systemctl --user enable firefox-profile@my_first_profile.timerBut 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
systemctl --user status your.serviceOffline
$ 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.timerLast edited by LithoUser (2022-02-13 07:20:39)
Offline
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.
Online
Thanks a lot, it seems to work now!
I'd still have two questions:
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
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")
Online
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
This shouldn't work:
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
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.targetOffline
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 %iNot 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.targetSince 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.timerI'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
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
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
Okay, you may be right about the '%i', yes.
You may show us what 'firefox-sync' looks like.
I gives us a better picture and others can test if they like;-)
Offline
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
See Raynman's post
Online
OK, so...
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 ![]()
Offline
[Unit]
Description=Firefox profile memory cache
[Service]
Type=oneshot
RemainAfterExit=false
ExecStart=/usr/local/bin/firefox-sync %ibasically the above, you could also just drop the RemainAfterExit line since false is the default.
Offline
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
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