Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
3  
I don't fully understand your requirements, please clarify. I understand that if you reboot your machine serveral times in a day, the script should only run after first reboot, is that right? If your machine runs for more than 1 day long, should the script be run in consecutive days? –  Krzysztof Adamski Sep 14 '12 at 18:29
    
Exactly. I will make a backup every day no matter how often I reboot my PC. –  Fu86 Sep 14 '12 at 22:42
5  
If you're not hung up on systemd, this is exactly what anacron is for. –  Gilles Sep 14 '12 at 23:48
    
I may be wrong but there's no easy way to set anacron to run script only 10 minutes after boot is complete. –  Krzysztof Adamski Sep 15 '12 at 9:16
    
That's right. That's why I'm currently using anacron and the good old at, with a daily job like echo "obnam backup" | at "now + 10 minutes" –  Adrien Clerc Sep 14 '13 at 10:33
add comment

3 Answers 3

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.