我有一個 systemd 服務,我需要在網路啟動之前運行它,但我對此有點掙扎。根據這個: 網路通後運轉服務我需要使用之前=network-pre.target,但是我的服務沒有啟動。
依賴項:
root@server:~# systemctl list-dependencies my-script --reverse
my-script.service
● └─network-pre.target (has a red dot next to it)
實際單位本身:
[Unit]
Description=My script
Before=network-pre.target
Wants=network-pre.target
[Service]
ExecStart=/etc/my-script
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
有什麼建議麼?
答案1
刪除 Wants= 行,因為您列出了無法滿足的內容:在 network-pre.target 之前啟動服務,但也在 (Wants) 之後啟動服務。所以單位應該讀作:
[Unit]
Description=My script
Before=network-pre.target
[Service]
ExecStart=/etc/my-script
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
答案2
這對我有用:
[Unit]
Description=@CPACK_PACKAGE_DESCRIPTION_SUMMARY@
Before=network-pre.target
Wants=network-pre.target
DefaultDependencies=no
Requires=local-fs.target
After=local-fs.target
[Service]
Type=oneshot
ExecStart=/sbin/my-script.sh
RemainAfterExit=yes
[Install]
WantedBy=network.target