this post was submitted on 07 Dec 2023
36 points (97.4% liked)

Linux

48083 readers
753 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

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.

[Unit]
Description=Run backup weekly
Requires=backup.service

[Timer]
Unit=backup.service
OnCalendar=weekly
RandomizedDelaySec=120
Persistent=true

[Install]
WantedBy=timers.target
top 6 comments
sorted by: hot top controversial new old
[–] Strit@lemmy.linuxuserspace.show 10 points 11 months ago (1 children)

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.

[–] Scholars_Mate@lemmy.world 15 points 11 months ago* (last edited 11 months ago) (1 children)

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.

This is not correct. Persistent=true saves the last time the timer was run on disk. From the systemd.timer man page:

Takes a boolean argument. If true, the time when the service unit was last triggered is stored on disk. When the timer is activated, the service unit is triggered immediately if it would have been triggered at least once during the time when the timer was inactive.

OP needs to remove Requires=backup.service from the [Unit] section so it stops running it when it start the timer on boot.

[–] PHLAK@lemmy.world 1 points 11 months ago

This is the correct answer.

[–] Scholars_Mate@lemmy.world 9 points 11 months ago

You have the timer requiring backup.service, so it will run that service every time the timer starts on boot. Remove Requires=backup.service, and that will fix the issue.

[–] laskobar@feddit.de 9 points 11 months ago

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.

[–] meyotch 2 points 11 months ago

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.