![systemd 用户计时器不重复](https://linux22.com/image/174664/systemd%20%E7%94%A8%E6%88%B7%E8%AE%A1%E6%97%B6%E5%99%A8%E4%B8%8D%E9%87%8D%E5%A4%8D.png)
我试图以用户身份每分钟运行一个 systemd 计时器,但在初始触发后它不会重复。配置ffmpeg-timelapse.timer
为OnCalendar=minutely
每分钟触发一次,并且ffmpeg-timelapse.target
是WantedBy
依赖服务。这使我可以轻松地从延时摄影配置中添加/删除摄像机。
我遇到的问题是,当我启动ffmpeg-timelapse.timer
设备时,它将安排在下一分钟,但不会重复。如果我从论证开始,也会出现同样的问题--now
。
ffmpeg-timelapse.timer
[Unit]
Description=Runs ffmpeg timelapse units every minute
[Timer]
OnCalendar=minutely
Unit=ffmpeg-timelapse.target
[Install]
WantedBy=timers.target
ffmpeg-timelapse.target
[Unit]
Description=Triggers the individual timelapse units for each camera.
StopWhenUnneeded=yes
相机服务文件的示例。
ffmpeg-timelapse01-front-yard.service
[Unit]
Description=Front Yard Timelapse Unit
Wants=ffmpeg-timelapse.timer
[Service]
ExecStart=/bin/bash -ac '. camera01.conf ; exec ffmpeg-timelapse.sh'
[Install]
WantedBy=ffmpeg-timelapse.target
启用并启动该服务会将其安排在下一分钟。
$ systemctl --user start ffmpeg-timelapse.timer
$ systemctl --user list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
Sat 2020-06-06 12:08:00 EDT 12s left n/a n/a ffmpeg-timelapse.timer ffmpeg-timelapse.target
1 timers listed.
Pass --all to see loaded but inactive timers, too.
然而,一旦运行它就不会再次触发。
$ systemctl --user list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
n/a n/a Sat 2020-06-06 12:08:42 EDT 1min 2s ago ffmpeg-timelapse.timer ffmpeg-timelapse.target
1 timers listed.
Pass --all to see loaded but inactive timers, too.
我正在运行此命令的用户已linger
启用。
$ loginctl show-user timelapse
UID=1000
GID=1000
Name=timelapse
Timestamp=Tue 2020-04-07 16:16:20 EDT
TimestampMonotonic=3291000946930
RuntimePath=/run/user/1000
[email protected]
Slice=user-1000.slice
Display=411982
State=active
Sessions=412092 411982 163185
IdleHint=no
IdleSinceHint=0
IdleSinceHintMonotonic=0
Linger=yes
状态输出对我来说看起来是正确的。
$ systemctl --user status ffmpeg-timelapse.target
● ffmpeg-timelapse.target - Triggers the individual timelapse units for each camera.
Loaded: loaded (/home/timelapse/.config/systemd/user/ffmpeg-timelapse.target; static; vendor preset: enabled)
Active: active since Sat 2020-06-06 10:50:42 EDT; 1h 23min ago
$ systemctl --user status ffmpeg-timelapse.timer
● ffmpeg-timelapse.timer - Runs ffmpeg timelapse units every 5 minutes
Loaded: loaded (/home/timelapse/.config/systemd/user/ffmpeg-timelapse.timer; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-06-06 12:13:25 EDT; 1min 1s ago
$ systemctl --user status ffmpeg-timelapse01-front-yard.service
● ffmpeg-timelapse01-front-yard.service - Front Yard Timelapse Unit
Loaded: loaded (/home/timelapse/.config/systemd/user/ffmpeg-timelapse01-front-yard.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sat 2020-06-06 12:14:03 EDT; 35s ago
Process: 4491 ExecStart=/bin/bash -ac '. camera01.conf ; exec ffmpeg-timelapse.sh'
Main PID: 4491 (code=exited, status=0/SUCCESS)
下面是输出journalctl -xe
Jun 06 12:13:25 srv01 systemd[26482]: Started Runs ffmpeg timelapse units every minute.
-- Subject: Unit UNIT has finished start-up
-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- Unit UNIT has finished starting up.
--
-- The start-up result is done.
Jun 06 12:14:02 srv01 systemd[26482]: Started Front Yard Timelapse Unit.
-- Subject: Unit UNIT has finished start-up
-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- Unit UNIT has finished starting up.
--
-- The start-up result is done.
答案1
我通过查看systemctl --user list-units --all ffmpeg*
输出确定了问题。剩余ffmpeg-timelapse.target
负载/活动/活动。在实际触发事件之前,ffmpeg-timelapse.timer
将其SUB
设置为waiting
。
UNIT LOAD ACTIVE SUB JOB DESCRIPTION
ffmpeg-timelapse.target loaded active active start Triggers the individual timelapse units for each camera.
ffmpeg-timelapse.timer loaded active running Runs ffmpeg timelapse units every minute
故障出在[Unit]
的配置上ffmpeg-timelapse.target
。我需要oneshot
向其中添加配置,否则目标单元仍处于活动状态。
$ cat ffmpeg-timelapse.target
[Unit]
Type=oneshot
Description=Triggers the individual timelapse units for each camera.
StopWhenUnneeded=yes
现在它按预期每分钟重复一次。
$ systemctl --user list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
Sat 2020-06-06 12:58:00 EDT 13s left Sat 2020-06-06 12:57:42 EDT 3s ago ffmpeg-timelapse.timer ffmpeg-timelapse.target
这就是单位现在的样子。
UNIT LOAD ACTIVE SUB DESCRIPTION
ffmpeg-timelapse01-front-yard.service loaded inactive dead Front Yard Timelapse Unit
ffmpeg-timelapse.target loaded inactive dead Triggers the individual timelapse units for each camera.
ffmpeg-timelapse.timer loaded active waiting Runs ffmpeg timelapse units every minute
每一项的状态输出。
$ systemctl --user status ffmpeg-timelapse.timer
● ffmpeg-timelapse.timer - Runs ffmpeg timelapse units every minute
Loaded: loaded (/home/timelapse/.config/systemd/user/ffmpeg-timelapse.timer; enabled; vendor preset: enabled)
Active: active (waiting) since Sat 2020-06-06 12:59:30 EDT; 3min 43s ago
$ systemctl --user status ffmpeg-timelapse.target
● ffmpeg-timelapse.target - Triggers the individual timelapse units for each camera.
Loaded: loaded (/home/timelapse/.config/systemd/user/ffmpeg-timelapse.target; static; vendor preset: enabled)
Active: inactive (dead) since Sat 2020-06-06 13:03:11 EDT; 6s ago
$ systemctl --user status ffmpeg-timelapse01-front-yard.service
● ffmpeg-timelapse01-front-yard.service - Front Yard Timelapse Unit
Loaded: loaded (/home/timelapse/.config/systemd/user/ffmpeg-timelapse01-front-yard.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sat 2020-06-06 13:03:12 EDT; 8s ago
Process: 9607 ExecStart=/bin/bash -ac '. camera01.conf ; exec ffmpeg-timelapse.sh'
Main PID: 9607 (code=exited, status=0/SUCCESS)