该单元文件应该是这样的:
[Unit]
Description=@CPACK_PACKAGE_DESCRIPTION_SUMMARY@
Before=network-pre.target
Wants=network-pre.target
[Service]
Type=oneshot
ExecStart=/sbin/my-script.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
根据http://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/
想要在网络配置之前运行的服务应该放置 Before=network-pre.target,并设置 Wants=network-pre.target 以将其拉入。
但它完全阻止了网络服务的运行。
答案1
这对我有用:
[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