我编写了一个非常简单的脚本 ( 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.
我究竟做错了什么?有没有更好的方法来实现我连续运行脚本的初始目标?
您的
.timer
单元(不是.service
单元,它有一个但可能不应该)缺少一个[Install]
部分。您可能要添加:
您的
.service
文件旨在仅由计时器激活,而不是直接在引导期间(等)激活。所以它不应该有一个[Install]
部分(也不应该是systemctl enable
'd)。