基于实例名称并带有计算的 Systemd 定时器 OnCalendar

基于实例名称并带有计算的 Systemd 定时器 OnCalendar

我有一个模板化的 systemd 计时器。实例名称全部都是整数([email protected]等等)。

在 OnCalendar 指令中,是否可以让日历表达式执行基本的数学运算%i

我想要做的基本上是将值乘以%i5,这样我就可以为计时器提供一个模板,并将它们的开始时间偏移 5 分钟%i,而不是为每个计时器创建单独的计时器单元。

我的想法是它可能看起来像这样:

OnCalendar=*-*-* 00:$$(%i * 5):00

我知道这种语法是非法的,因为我在我制作的沙盒计时器单元中尝试过它,但是,有没有办法使用单个模板计时器来完成我想做的事情?

答案1

虽然我不知道任何数学能力,但也许这可以满足您的需求:

您可以添加RandomizedDelaySec到计时器模板中,该模板在计时器之间平均分配开始时间。

   RandomizedDelaySec=
      Delay the timer by a randomly selected, evenly distributed
      amount of time between 0 and the specified time value.
      Defaults to 0, indicating that no randomized delay shall be
      applied. Each timer unit will determine this delay randomly
      before each iteration, and the delay will simply be added on
      top of the next determined elapsing time, unless modified
      with FixedRandomDelay=, see below.

      This setting is useful to stretch dispatching of similarly
      configured timer events over a certain time interval, to
      prevent them from firing all at the same time, possibly
      resulting in resource congestion.

此外,FixedRandomDelay可以设置为true,以保留随机延迟并在将来的运行中重新使用它。

   FixedRandomDelay=
       Takes a boolean argument. When enabled, the randomized offset
       specified by RandomizedDelaySec= is reused for all firings of
       the same timer. For a given timer unit, the offset depends on
       the machine ID, user identifier and timer name, which means
       that it is stable between restarts of the manager. This
       effectively creates a fixed offset for an individual timer,
       reducing the jitter in firings of this timer, while still
       avoiding firing at the same time as other similarly
       configured timers.

来源:https://www.man7.org/linux/man-pages/man5/systemd.timer.5.html又名man systemd.timer

相关内容