我编写了一个非常简单的脚本(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]
部分。
您可能要添加:
[Install]
WantedBy=timers.target
你的.service
文件旨在引导(等)期间,由定时器才会被激活,而不是直接。因此,它不应该有一个[Install]
部分(也不应该是systemctl enable
'd)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句