Score:2

Running a systemd job with a delay when activated manually, not at boot

lk flag

I am trying to set up a systemd job to run once, with a delay, after activating it specifically. I do not want it to run on boot. I had thought that the following timer file would do the trick:

[Unit]
Description=Tests whether onactivesec runs at boot

[Timer]
OnActiveSec=2min
Persistent=false

[Install]
WantedBy=timers.target

Now, that works in that it does what I need it to do: if I activate it manually, its associated service file runs 2 minutes later. However, it also triggers when the computer boots up so that the service file runs 2 minutes after boot as well.

Is there a way of tweaking this so that it runs with a delay when specifically activated but does not run on boot?

Marco avatar
br flag
Have you tried adding `OnBootSec=100y` ?
Adam Jacobs avatar
lk flag
Well, I tried setting OnBootSec=60h, which I guess would have a similar effect. It still activates 2 min after boot, though it does also set itself for 60 h after boot as well.
Adam Jacobs avatar
lk flag
It occurs to me that I could set up another systemd timer to run 1 min after boot that would then run a script that runs "systemctl stop" on the first systemd timer. I guess that would probably work, but it seems like a rather inelegant workaround rather than a proper solution.
Score:2
ec flag

systemd Timer Units are designed for running Service Units periodically, whereas periodically includes "after every boot process".

Two possible solutions for you:

  • deactivate the units after it ran, this will prevent that it runs at the next reboot / system start.
  • discard the timer unit and add to your service unit the directives TimeoutStartSec=125 and ExecStartPre=/bin/sleep 120 to make the unit sleep for 120 seconds before the ExecStart directive is run. The TimeoutStartSec directive is necessary because Service units gets terminated by systemd when the actual command / program to be executed does not start within 90 seconds.
Adam Jacobs avatar
lk flag
Thank you, your first suggestion works well. I have a script that runs just before shutdown, and I've simply added a few lines to that script to disable the relevant timers. The script that starts the timers again just needs an extra line to enable them first. Everything working as intended now :)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.