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]

위의 두 가지를 활성화했습니다 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).

관련 정보