49

I have a timer/service unit-set that should run once a day under --user conditions. It shows up with systemctl --user status and gets logged in journal but there is a part of the command that fails.

It seems that something in the command is not being interpreted correctly. I want to futz with the unit file and run the service, examine the log, etc to debug the issue; however editing the timer to trigger a minute in the future, waiting, and checking the log is... tedious.

Can do something like systemctl --user execute xxxxxx.service to just run the dang thing as if the timer triggered?

Gus
  • 643
  • 1
  • 6
  • 9

1 Answers1

33

You can activate any unit manually, unless it contains a RefuseManualStart=yes and/or RefuseManualStop=yes directive (which do exactly what they say). Just issue systemctl --user start <whatever> (and systemctl --user stop <whatever> to do the opposite).

To quote systemctl(1):

start PATTERN...

Start (activate) one or more units specified on the command line.

<...>

stop PATTERN...

Stop (deactivate) one or more units specified on the command line.


Note that manually starting the timer's paired unit does not affect the timer's own state (i. e. it won't change the timer's LastTrigger* or NextElapse* properties) and does not change the time it will next elapse. Forcibly elapsing a timer is not supported in systemd.

intelfx
  • 5,365
  • Not working here: Failed to restart {foo}.service: Operation refused, unit {foo}.service may be requested by dependency only. – eMPee584 Jul 09 '19 at 18:01
  • 1
    @eMPee584 Since this answer was written, systemd gained new directives RefuseManualStart= and RefuseManualStop= (which do exactly what they say). I've updated my answer to mention that. – intelfx Jul 29 '19 at 03:13
  • 1
    I tried this on the fstrim.timer. But for the timer it doesn't do anything. (enables it I think) When issued on fstrim.service, probably something happened. Is there no way to trigger the timer so it shows it was actually executed? – JPT Apr 10 '20 at 12:13
  • 15
    @JPT "starting" a timer means that the timer starts ticking. Forcibly elapsing a timer is not supported in systemd. If you need this, just start the paired unit directly (in your case, fstrim.service). – intelfx Apr 12 '20 at 03:58
  • 1
    So no, there is no way to manually trigger a timer. You may want to work that information into the answer, since it's what is actually answering the question. – miken32 Jan 17 '23 at 21:17
  • Doesn't work: even though the command succeeds, doing a systemctl --user status foo.timer says Active: … 18min ago. And the time in logs says similarly Apr 06 15:15:26 … Started run my script. whereas the current time is ≈15:30. – Hi-Angel Apr 06 '23 at 12:37
  • @Hi-Angel Yes, manually starting the paired unit does not do anything to the timer's own state. As said above, forcibly elapsing a timer is not supported in systemd. I have clarified this in the answer now. – intelfx Apr 09 '23 at 11:06