I want to run a backup script 10 minutes after booting up my machine but only once a day. Is it possible to build up such scenarios with systemd?
4 Answers
See: /lib/systemd/system/systemd-tmpfiles-clean.timer
:
[Timer]
OnBootSec=15min
OnUnitActiveSec=1d
This runs the corresponding .service
file 15 minutes after boot and then every day while the system is up. If you reboot multiple times in the same day, you can just have the backup script check the mtime of the archive and skip that run if it's less than a day old.
Also, if your backups are going to be heavy on IO, it might be helpful to read about the IOScheduling* directives in the systemd.exec(5) manpage.
I don't think it's possible to do this entirely in systemd as it does not track any state between reboots. You can easly schedule something to run 10 minutes after boot using systemd.timer
and its OnBootSec=
or OnStartupSec=
.
The script itself has to check if it was already run today or not. This can be easily implemented even in bash, for example:
#/bin/sh -x
FILE=/etc/lastrun
TODAY=`date +"%Y%m%d"`
LASTRUN=`cat $FILE`
[[ -z "$LASTRUN" ]] || [[ "$TODAY" -gt "$LASTRUN" ]] || exit
echo $TODAY > $FILE
RUNYOURSCRIPTHERE
Apparently this issue will be addressed in future releases. At least according to this email on the systemd-devel mailing list. Until that you have to go the hackish way Krzysztof proposed.
If you're not hung up on 10 min after boot, you can:
[Unit]
Description=...
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
OnCalendar=daily
makes it run every midnight, Persistent=true
on boot if there was at least one missed run.
echo "obnam backup" | at "now + 10 minutes"