我有一个 systemd 服务,需要在网络启动之前运行它,但我对此有点困惑。根据以下内容: 网络启动后运行服务我需要使用之前=网络预.目标,但我的服务尚未启动。
依赖项:
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