我编写了一个非常简单的脚本 ( checkaudio.sh
),它从 mqtt 主题的文件中发布消息。我希望脚本能够连续运行(即使每一秒我都会很高兴)。我首先尝试使用 cron,这在技术上是可行的,但作为解决方案是“肮脏的”(多个 cron 作业,每个作业有 1 秒的延迟)。
我已经尝试过systemd
它及其timer
功能。我对 systemd 不是很精通,这就是我想出的:
/etc/systemd/system/[email protected]
内容:
[Unit]
Description=Announce every second
[Install]
WantedBy=default.target
[Service]
Type=oneshot
ExecStart=/root/checkaudio.sh
/etc/systemd/system/[email protected]
内容:
[Timer]
OnUnitActiveSec=1s
AccuracySec=1ms
[email protected]
我通过激活上面两个systemctl enable
。一切都运行顺利,直到我重新启动系统并且无法再启用。我收到以下错误:/etc/systemd/system/[email protected]
The unit files have no installation config (WantedBy, RequiredBy, Also, Alias
settings in the [Install] section, and DefaultInstance for template units).
This means they are not meant to be enabled using systemctl.
Possible reasons for having this kind of units are:
1) A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).
4) In case of template units, the unit is meant to be enabled with some
instance name specified.
我究竟做错了什么?有没有更好的方法来实现我连续运行脚本的最初目标?
答案1
您的.timer
设备(不是该.service
设备,它有一个但可能不应该)缺少一个[Install]
部分。
您可能想添加:
[Install]
WantedBy=timers.target
您的.service
文件仅由计时器激活,而不是在引导期间(等)直接激活。所以它不应该有一个[Install]
部分(也不应该是systemctl enable
“d”)。