On Fri, Oct 1, 2021 at 3:37 PM antlists <antli...@youngman.org.uk> wrote:
> I'm trying to get a systemd unit to fire on boot once a week. Reading up > on timer units, I can't work out how to get it to work. > > This is tied up with my earlier systemd mount post - I've now got that > sorted - I've got dm-integrity to fire before fstab. > > I now want to run lvm snapshot on the first boot of the weekend. Writing > a unit to do the snapshot seems pretty easy, but obviously I don't want > it firing every boot, if I stick the date in the volume name I don't > want it colliding with an earlier run the same day, etc etc. > > The question really is - if I have a weekly timer fire and activate the > unit, is the activation going to survive the reboot to run on the next > boot? > > The problem I'm having is that all the stuff I've seen about timers says > you have two lines - activate on Saturdays, and activate on boot. Snag > is, they seem to be independent such that EITHER condition will activate > the service. As I say, I want BOTH. I don't want the service running > while the system is up and running. > I'm not sure timer units have enough flexibility to do what you want. I think it would be much simpler to have a Type=oneshot service at boot, and the Exec= line to call a script. You can store the timestamp of the last time it was called someplace in the filesystem (say, /var/lib/my-script or something), and if the timestamp doesn't exists or is older than one week, the scripts executes lvm snapshot and updates the timestamp; otherwise it ends without doing anything. The timer units are very similar to cron, and I believe what you want cannot be done with cron either; you need special logic and state ("I'm booting AND haven't run this in at least a week"), so a script is necessary (I think). Luckly, systemd allows you to smartly manage your scripts and impose dependencies on them (you need /var in my example, and you can set it to run before starting X). Regards. -- Dr. Canek Peláez Valdés Profesor de Carrera Asociado C Departamento de Matemáticas Facultad de Ciencias Universidad Nacional Autónoma de México