[systemd-devel] Periodic tasks on battery-powered devices

Lennart Poettering lennart at poettering.net
Mon Feb 25 13:04:09 UTC 2019


On So, 24.02.19 18:19, David Strauss (david at davidstrauss.net) wrote:

> I could be missing something, but I think there's a gap in the
> functionality for timers for periodic tasks on battery-powered devices.
> Basically, I want to generate a security report (SCAP style) approximately
> weekly but avoid doing so on battery.
>
> Here's what I've tried or looked at:
>
> - If I use ConditionACPower=true on the Type=OneShot service performing the
> task, then systemd will not run the task while on battery, but it still
> resets the timer. With a longer OnCalendar= interval, there's a significant
> chance that the infrequent check will overlap with battery-mode operation,
> causing the task to never run.
>
> - If I use ConditionACPower=true on the timer, then the timer just won't
> start if the machine boots on battery. This is also undesirable, as a
> machine may only boot every few weeks.
>
> - OnActiveSec= doesn't support persistence, so intervals of approximately a
> week may never fire on some laptops that shutdown or reboot frequently.
>
> - OnBootSec= would be too frequent for laptops that shutdown or reboot
> frequently.
>
> It seems the best I can do right now is use a shorter timer interval with
> no AC power condition but have the Type=OneShot service exit early if it
> last ran too recently, especially if currently on battery power. I'm also
> looking at just using anacron.
>
> What I would prefer is a [Timer] option like AvoidOnBatterySec= that causes
> the timer to avoid firing on battery yet fire on restoration of AC power if
> overdue. If the AvoidOnBatterySec= duration has elapsed since the last
> firing, the timer fires regardless of whether the device is on battery.
> This would allow easy configuration of periodic tasks that are power-hungry
> but need to run eventually. Some of our laptops rarely run on AC aside from
> when they're closed at night.

There has been a feature request since a long time to add something
like this (see TODO): a scheme to dynamically prolong time intervals
when on battery, but bound it.

i.e. to say something like: "run this every 5min, but on battery
prolong this to 5h". I think adding this would make a lot of
sense. This just needs somebody to do the work and implement it ;-)

It's not entirely trivial to implement, since it means systemd would
have to watch AC power state, which means watching all power source
devices for uevents, and then rescheduling timer events whenever
things change, and all that without strictly relying on udev to be
started, since it's not going to be started during early boot...

ConditionACPower= is indeed not useful for stuff like this.

Lennart

--
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list