[systemd-devel] Stop systemd timer upon success script and restart the next day

Lennart Poettering lennart at poettering.net
Thu May 16 09:29:52 UTC 2019


On Mi, 15.05.19 16:21, Jeffrey Walton (noloader at gmail.com) wrote:

> I have a systemd timer that starts a service, and the service executes
> a script that downloads data files once a day. Once the data files are
> retrieved I don't need the timer for the remainder of the day.
> However, I need the time again the next day.
>
> Here are the two docs I found on scheduling a timer, but I was not
> able to parse out the info I needed.
> https://www.freedesktop.org/software/systemd/man/systemd.timer.html
> and https://www.freedesktop.org/software/systemd/man/systemd.time.html
>
> How do I specify a timer that starts 6:00 AM every morning, fires once
> an hour, and then stops for the day upon success of the download?

define two timer units and one service:

1. six-o-clock.timer with contents like this:

        [Timer]
        OnCalendar=6:00
        Unit=myapp.service

        [Install]
        WantedBy=timers.target

2. every-hour.timer with contents like this:

        [Timer]
        OnUnitActive=1h
        Unit=myapp.service

        [Install]
        Also=six-o-clock.timer

3. myapp.service with contents like this:

        [Unit]
        Wants=every-hour.timer
        After=every-hour.timer

        [Service]
        ExecStart=/path/to/my/script.sh

        [Install]
        Also=six-o-clock.timer

4. Inside your script script.sh you should then do your downloads. If
   anything fails you just let the script abort, knowing that
   every-hour.timer will start it again after one hour. On success
   however, you invoke "systemctl stop every-hour.timer" to shut down
   this timer again, leaving only "six-o-clock.timer" running.

By using two separate timer units you can schedule the service based
on two separate expressions that you can turn off and on individually
just by starting or stopping the timer.

six-o-clock.timer is the timer started during boot (since after
enablement is pulled in by timers.target which is the generic target
timers should be plugged into if they shall be started at boot). Then,
every-hour.timer is pulled in together with myapp.service the instant
it is requested to ensure that from that point on the every hour thing
applies, until its turned off again because the timer is stopped by
your script.

Lennart

--
Lennart Poettering, Berlin


More information about the systemd-devel mailing list