systemdを使用して継続的にスクリプトを実行する方法

systemdを使用して継続的にスクリプトを実行する方法

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]

上記の2つを有効にしました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](または「d」があってはいけませんsystemctl enable)。

おすすめ記事