After=multi-user.target やその他のシステムサービスで動作しない

After=multi-user.target やその他のシステムサービスで動作しない

動作するサービスがあり、dhcpcd.servicegithubプルを実行し、DHCPが機能しない場合はホストを確認できないため、実行後にのみ機能します。network.targetこれは、DHCP サービスが完了する前に実行されます。サービスが実行されていると(サーバー)ユーザーはmichaelログインしません。

問題は、すべてのケースを処理するために非常に長く醜い遅延を使用せずに作業できるほど遅く実行できないことです。

ExecStartPre=sleep 30

サービスは次のとおりですupdatecontinue.service(私が試したいくつかの組み合わせの1つAfter=)。

# man systemd.service
# man systemd.unit
#
# Debug with (will print errors at the bottom, time is in UTC!):
#   -n50 to show 50 lines
#   sudo systemctl status updatecontinue
#
# Or to get a plot (open with chrome so its searchable):
#   systemd-analyze plot > ~/plot.svg

[Unit]
Description=Check if an update is halfway through, if yes, then update/enable OverlayFS/reboot

[Service]
Type=simple
ExecStart=/home/michael/.venv/terminal/bin/python3 /home/michael/terminal/script/update.py --check
After=dhcpcd.service
User=michael

[Install]
WantedBy=multi-user.target

ここで定義された場所から始まらないことがわかります。

ノート

次のように設定しても:

After=dhcpcd.service systemd-update-utmp-runlevel.service systemd-timesyncd.service

ここに画像の説明を入力してください。

RON推奨注意事項

systemctl get-defaultレポートgraphical.target

このサービスを利用してください:

[Unit]
Description=Check if an update is halfway through, if yes, then update/enable OverlayFS/reboot

[Service]
Type=idle
ExecStart=/home/michael/.venv/terminal/bin/python3 /home/michael/terminal/script/update.py --check
After=default.target
TimeoutStartSec=0
User=michael

[Install]
WantedBy=default.target

結果:

ここに画像の説明を入力してください。

ベストアンサー1

systemdが解決しようとするいくつかの競合を定義してWantedBy=multi-user.target追加しました。After=multi-user.targetこの回答の下でより大きな説明/実験を見ることができます。

代わりにサービスが早すぎる場合は、サービスが失敗する理由を見つけてください。実行する代わりにサービス/ターゲットをAfter=multi-user.target実行してください。After=

After=network.target私の考えでは、唯一の問題がネットワークであれば追加します。

ユーザーログインが必要な場合はバスを使用してください--user

グラフィックセッションが必要な場合は、WantedBy=graphical.targetシステムバスまたはWantedBy=graphical-session.targetユーザーバスを検討してください。


使用すると関係をWantedBy=multi-user.target結びます。multi-user.targetWants=updatecontinue.service

これはただ始まるとupdatecontinue.service始まるという意味です。起動に失敗しmulti-user.targetてもupdatecontinue.service問題はありませんmulti-user.target

updatecontinue.serviceその後、関係を提供しますRequires=multi-user.target。これは重大な紛争です。 (起動するRequires=Wants=これそれも始まります)。例外:それデバイスをアクティブにできません。これデバイスが起動しません。

したがって、実際には互いに起動する2つのデバイスしかありませんが、これはおそらく望むものではありません。Requires=明確にするために依存関係を削除します。


Before/関係は機能します。以下はAfter/なしで対話する2つのサービスの例です。BeforeAfter

$ systemctl --user cat {early,late}.service
# /home/stew/.config/systemd/user/early.service
[Unit]
Wants=late.service

[Service]
ExecStartPre=sleep 2
ExecStart=sleep 20

# /home/stew/.config/systemd/user/late.service
[Service]
ExecStart=sleep 5

$ systemctl --user start early
$ journalctl --user --since "2 minutes ago" --no-hostname
Mar 08 15:54:42 systemd[1064]: Starting early.service...
Mar 08 15:54:42 systemd[1064]: Started late.service.
Mar 08 15:54:44 systemd[1064]: Started early.service.

同時にサービスが開始されることを確認できます。

late.serviceそれでは関係を築きましょうAfter=early.service

$ systemctl --user cat {early,late}.service
# /home/stew/.config/systemd/user/early.service
[Unit]
Wants=late.service
[Service]
ExecStartPre=sleep 2
ExecStart=sleep 20

# /home/stew/.config/systemd/user/late.service
[Unit]
After=early.service
[Service]
ExecStart=sleep 5

$ systemctl --user start early
$ journalctl --user --since "2 minutes ago" --no-hostname
Mar 08 16:01:09 systemd[1064]: Starting early.service...
Mar 08 16:01:11 systemd[1064]: Started early.service.
Mar 08 16:01:11 systemd[1064]: Started late.service.

この場合、完了がlate.service始まったことがわかります(スタンバイセルになります)。early.serviceExecStartPreactive (running)


サービスとは異なり、ターゲットは「開始済み」状態に移行しません。代わりに、彼らは「達成」された。 andを使って同じことを試してみましょうearly.targetlate.serviceしかし、late.service始めるのに少し時間がかかります。

$ systemctl --user cat early.target late.service
# /home/stew/.config/systemd/user/early.target
[Unit]
Wants=late.service

# /home/stew/.config/systemd/user/late.service
[Service]
ExecStartPre=sleep 2
ExecStart=sleep 5

$ systemctl --user start early.target
$ journalctl --user --since "2 minutes ago" --no-hostname
Mar 08 16:07:27 systemd[1064]: Starting late.service...
Mar 08 16:07:29 systemd[1064]: Started late.service.
Mar 08 16:07:29 systemd[1064]: Reached target early.target.

early.target「到着」に2秒かかることがわかります。これは、すべての目標の「Wants =」が「アクティブ化」ステップを完了し、「アクティブ(実行中)」状態に入ったときにのみ、ターゲットが「達成」されることを意味します。

これを知ることで、After=foo.targetaとの混合がWantedBy=foo.targetやや矛盾する理由がわかります。 foo.target到着前に始めるのを待っています。その間は、foo.target始める前にあなたに連絡したいと思います。

おすすめ記事