システムサービスで「After」キーワードはどのように機能しますか?

システムサービスで「After」キーワードはどのように機能しますか?

a.serviceとb.serviceの2つのサービスを用意しました。 a.service は a.timer を使用して実行されます。

サービス

[Unit]
Description=service a
After=network-online.target
Wants=network-online.target

[Service]
Type=simple

#restart on failure and tries 5 times. If fail in all, then reboot the system
ExecStart=/usr/bin/python /home/pi/.clar/a.py
Restart=on-failure
RestartSec=15
StartLimitBurst=5
StartLimitInterval=2min
StartLimitAction=reboot

a.タイマー

[Unit]
Description=10minute timer

[Timer]
# start this 20 Sec after boot:
OnBootSec=20

# ... and then every 10 minute:
OnUnitActiveSec=10m

[Install]
WantedBy=multi-user.target

b.サービス

[Unit]
Description=b service
After=a.service

[Service]
Type=simple

#restart on failure and tries 5 times. If fail in all, then reboot the system
ExecStartPre=/usr/bin/python /home/pi/.clar/c.py
ExecStart=/usr/bin/python /home/pi/.clar/b.py
Restart=on-failure
RestartSec=15
StartLimitBurst=5
StartLimitInterval=2min
StartLimitAction=reboot

[Install]
WantedBy=multi-user.target

私の設定では、a.serviceは起動から20秒後に起動し、b.serviceはa.serviceの後に実行する必要があります。

しかし、実際にはb.serviceはa.serviceの前に始まります。 Googleを検索してRequires=a.serviceb.serviceユニットセクションに追加しようとしました。Wants=a.serviceしかし、誰も私を助けませんでした。

退勤後は理解できません。設定に追加する必要がある他の項目はありますか?

ベストアンサー1

ムルが言ったように、以下のように修正できます。

[Unit]
Description=b service
After=a.service

[Service]
Type=simple

#restart on failure and tries 5 times. If fail in all, then reboot the system
ExecStartPre=/usr/bin/python /home/pi/.clar/c.py
ExecStart=/usr/bin/python /home/pi/.clar/b.py
Restart=on-failure
RestartSec=15
StartLimitBurst=5
StartLimitInterval=2min
StartLimitAction=reboot

[Install]
WantedBy=a.service

それからあなたはしなければなりませんsudo systemctl disable b.servicesudo systemctl enable b.service

おすすめ記事