Skip to content

Setting up a systemd timer for 2 hours after boot, and 2 hours thereafter

I have a .timer file, along with a .service file, that I set up in order to backup 2 hours after boot and then every 2 hours after that. I thought I set it up correctly, but my backups haven't run as intended. Would someone mind reviewing my .timer file and telling me where I screwed up? Thanks!

[Unit]
Description=Backup Laptop Home every 2 hours

[Timer]
OnBootSec=2 hr
OnActiveSec=2 hr

[Install]
WantedBy=default.target

When I run systemctl --user list-timers it does show it ran the first time, but hasn't any subsequent times. It's also noteworthy that it ran just fine yesterday when I set it up, and even ran correctly after a shutdown and a reboot.

EDIT: Is the following better notation?

[Timer]
OnCalendar=*:0/120

EDIT: Nvm on the previous edit, systemd did not like it. I went ahead and stopped and disabled it, then started and re-enabled to see if it will work again as it did the day before.

Edited 12 Oct 2016. We only hold this version.

2 points4 comments
Open on Reddit
About this post
Posted
Archived
Edited
Score
2 · 100% up
Kind
Text
ID
576ps2

Author

17
posts
174
comments
5
media
View profile

4 comments

After consulting man systemd.timer, I think you want OnUnitActiveSec= instead of OnActiveSec=. OnActiveSec= is relative to when the timer was activated, which I believe means when you start the timer unit, so this only differs from OnBootSec= by a few seconds if the timer is enabled to start on boot.

1 points

Currently, after stopping and restarting, my timer is looking to run ok. It ran once intially and now it's back to the countdown to the next 2 hour mark. I'm going to let it run as is then see what happens tomorrow, as it seems to have stopped working as intended on the following day. If it continues to give me problems I'll give your solution a shot.

1 points

When you restart the timer, it runs immediately because you're already more than two hours past boot, and the other run is scheduled for two hours later (two hours from when you started the timer). During a boot of the system, the timer is also started, but both runs are scheduled for two hours later (almost at the same time).

Also, I'm pretty sure OnActiveSec= won't give you the repeating-every-two-hours behavior that you want. It only gives you one additional run (because the timer is only started once). On the other hand, OnUnitActiveSec= is based on the time since the corresponding service last started, so it schedules another run two hours later each time the service runs.

1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.