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.
About this post
- Posted
- Archived
- Edited
- Score
- 2 · 100% up
- Kind
- Text
- ID
- 576ps2
4 comments
After consulting
man systemd.timer, I think you wantOnUnitActiveSec=instead ofOnActiveSec=.OnActiveSec=is relative to when the timer was activated, which I believe means when you start the timer unit, so this only differs fromOnBootSec=by a few seconds if the timer is enabled to start on boot.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.
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.