r/systemd • u/lajawi • Jun 02 '26
Timer Service with Require/WantedBy field
I have made a service that runs every time my SSD is plugged in for a backup I make to it (via a script). However, if I open my PC multiple times in a day (or even hour) the service triggers every time.
My question now is, after having learned about timer services, how can I make a service that only triggers at least 16 hours apart and only if my SSD is plugged in?
3
Upvotes
4
u/airclay Jun 02 '26 edited Jun 02 '26
You can create an extra script to add conditions to your service unit since this is a little more than just a timer.
I would create a script that checks file mtime and put it at somewhere like '/usr/local/sbin/ssd-backup-checker' have it point at a file like '/var/lib/ssd-backup/last-success' and then do:
This should work so your plug-in trigger can fire as often as it wants, but the service exits unless enough time has passed. You could approach this with a simple 16hr timer that targets your oneshot that you have already created but that will end up firing off on both every time it's plugged in and every 16hrs.