Systemd timer unit

I try to create a timer unit for weekly and daily backups. For example with the weekly unit, it should be executed once in a week, some minutes after login. If the unit was successful, it should shutdown and not start again until the next week. If a start of this unit was missing, it should be start again some minutes after the next login.

But for some unknown reason, the current unit starts after every login when I reboot the laptop. I am relatively sure that this timer unit is set up wrong, but unfortunately I don’t know how I can implement such a unit better with the functions mentioned above.


<span style="color:#323232;">[Unit]
</span><span style="color:#323232;">Description=Run backup weekly
</span><span style="color:#323232;">Requires=backup.service
</span><span style="color:#323232;">
</span><span style="color:#323232;">[Timer]
</span><span style="color:#323232;">Unit=backup.service
</span><span style="color:#323232;">OnCalendar=weekly
</span><span style="color:#323232;">RandomizedDelaySec=120
</span><span style="color:#323232;">Persistent=true
</span><span style="color:#323232;">
</span><span style="color:#323232;">[Install]
</span><span style="color:#323232;">WantedBy=timers.target
</span>
laskobar,

OK, many thx for the tips. Since my script in the service file is already doing some logging, i will try to use the last log entry, to find out, when it was last time running and exit the script, if it is not in the timeframe of 1 week.

Strit,
@Strit@lemmy.linuxuserspace.show avatar

the timer has no idea if it was triggered during last boot. It only has the context of “this” boot, so it will do it right after a reboot and set a timer to start the service again after a week of uptime.

So if you reboot every day, it will trigger the service every day, even though you set it to weekly in the timer.

So it’s up to your .service file to determine if it has been run this week or not.

meyotch,

Your systemd file looks ok, but I think it’s doing exactly what you are telling it.

The solution may lie in the backup.service. Is that code you can modify? The OnCalendar=weekly doesn’t specify when in the week the service should run so that config may be vague.

If I understand the desired function here, you will need the service up all the time. It will just wait politely and occasionally run the specific backup script. It’s up to the backup script to determine when the last backup was made and either exit early because it hasn’t been a week or run the backup and reset a flag file.

At least that’s the approach I would take. Systemd is a very vigilant, but very stupid, service manager. It just watches and triggers services based on just a few criteria. Any logic more complex needs to go in the service itself.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • linux@lemmy.ml
  • khanakhh
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • osvaldo12
  • ethstaker
  • Youngstown
  • mdbf
  • slotface
  • rosin
  • everett
  • ngwrru68w68
  • kavyap
  • InstantRegret
  • megavids
  • GTA5RPClips
  • Durango
  • normalnudes
  • cubers
  • tacticalgear
  • cisconetworking
  • tester
  • modclub
  • provamag3
  • anitta
  • Leos
  • JUstTest
  • lostlight
  • All magazines