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

2019-05-16 Thread Lennart Poettering
On Mi, 15.05.19 16:21, Jeffrey Walton (noloa...@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
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel

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

2019-05-15 Thread Mantas Mikulėnas
On Wed, May 15, 2019 at 11:21 PM Jeffrey Walton  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?
>

I think you only do "every hour 6:00 to XX:00" through systemd.timer and
the rest through script logic – simply make the script exit if it finds
today's data already having been downloaded.

-- 
Mantas Mikulėnas
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel

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

2019-05-15 Thread Andrei Borzenkov
15.05.2019 23:21, Jeffrey Walton пишет:
> 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?

You do not. Timers are not aware what services they start do, nor are
they aware of "download success".

How expensive starting of service is that you need to prevent it?

I was about to suggest transient timers to simulate "at" command - your
service could schedule next run if necessary. It almost works (i.e. next
service invocation is scheduled) but it results in permanent:

мая 16 08:31:34 bor-Latitude-E5450 systemd-run[12618]: Failed to start
transient timer unit: Unit test-service.timer already exists.

For a proof of concept

[Service]
ExecStart=/bin/echo I am started
ExecStopPost=/usr/bin/systemd-run --no-block --user
--unit=test-service.service --on-unit-inactive=+5


___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel

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

2019-05-15 Thread Jeffrey Walton
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?
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel