systemd 计时器在第一次尝试触发其关联的 oneshot 服务时卡住运行

systemd 计时器在第一次尝试触发其关联的 oneshot 服务时卡住运行

也许我误解了关联计时器如何处理“一次性”服务。这是一个虚拟单元和一个计时器,应该每 5' 触发一次(在systemd-247.3-7+deb11u1):

# systemctl cat dummy.timer dummy.service 
# /etc/systemd/system/dummy.timer
[Unit]
Description=Trigger dummy service every 5'

[Timer]
OnCalendar=*:0/5
Persistent=true

[Install]
WantedBy=timers.target

# /etc/systemd/system/dummy.service
[Unit]
Description=dummy one-shot service for timer testing

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=echo "Dummy start"
ExecStop=echo "Dummy stop"

[Install]
WantedBy=multi-user.target

虚拟服务不是守护进程,但它应该更改系统的状态,因此RemainAfterExitExecStop回滚ExecStart.

起初,一切看起来都很好:

# systemctl start dummy.timer dummy.service
... 
# systemctl status dummy.timer dummy.service 
● dummy.timer - Trigger dummy service every 5'
     Loaded: loaded (/etc/systemd/system/dummy.timer; disabled; vendor preset: enabled)
     Active: active (waiting) since Fri 2023-04-07 21:05:39 CEST; 1s ago
    Trigger: Fri 2023-04-07 21:10:00 CEST; 4min 19s left
   Triggers: ● dummy.service

● dummy.service - dummy one-shot service for timer testing
     Loaded: loaded (/etc/systemd/system/dummy.service; disabled; vendor preset: enabled)
     Active: active (exited) since Fri 2023-04-07 21:05:39 CEST; 1s ago
TriggeredBy: ● dummy.timer
    Process: 1805980 ExecStart=echo Dummy start (code=exited, status=0/SUCCESS)
   Main PID: 1805980 (code=exited, status=0/SUCCESS)
        CPU: 1ms

但是,计时器不会触发并陷入“运行”状态:

# systemctl status dummy.timer dummy.service 
● dummy.timer - Trigger dummy service every 5'
     Loaded: loaded (/etc/systemd/system/dummy.timer; disabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-04-07 21:05:39 CEST; 35min ago
    Trigger: n/a
   Triggers: ● dummy.service

● dummy.service - dummy one-shot service for timer testing
     Loaded: loaded (/etc/systemd/system/dummy.service; disabled; vendor preset: enabled)
     Active: active (exited) since Fri 2023-04-07 21:05:39 CEST; 35min ago
TriggeredBy: ● dummy.timer
    Process: 1805980 ExecStart=echo Dummy start (code=exited, status=0/SUCCESS)
   Main PID: 1805980 (code=exited, status=0/SUCCESS)
        CPU: 1ms

Apr 07 21:05:39 myvps echo[1805980]: Dummy start

# systemctl list-timers dummy
NEXT LEFT LAST                         PASSED    UNIT        ACTIVATES
n/a  n/a  Fri 2023-04-07 21:10:09 CEST 30min ago dummy.timer dummy.service

看起来计时器不将“活动(退出)”理解为终止。我正在尝试一些不可能的事情吗?不然就有腥味了。

答案1

看起来计时器不将“活动(退出)”理解为终止。

因为单位是积极的。进程是否终止与定时器无关;重要的是该单元作为一个整体是否仍处于“活动”状态,这意味着它不能再次激活——至少在不先停止它的情况下不能。

当然,该单位仍处于“活动”状态,因为您有RemainAfterExit=yes.

如果您希望计时器定期重新应用更改,则需要删除该选项并将服务拆分为两个常规一次性服务,一个用于应用状态,另一个用于取消应用状态。

相关内容