无法启动 systemd 服务

Posted

技术标签:

【中文标题】无法启动 systemd 服务【英文标题】:Failing to start systemd service 【发布时间】:2015-11-17 04:00:51 【问题描述】:

我编写了以下 systemd 服务以在启动时登录无线:

[Unit]
Description=Wireless network connectivity (%i)
Wants=network.target
Before=network.target
BindsTo=sys-subsystem-net-devices-%i-device
After=sys-subsystem-net-devices-%i-device

[Service]
Type=oneshot
RemainAfterExit=yes

ExecStart=/usr/bin/ip link set dev %i up
ExecStart=/usr/bin/wpa_supplicant -B -i %i -c /etc/wpa_supplicant/wpa_supplicant.conf
ExecStart=/usr/bin/dhcpcd %i

ExecStop=/usr/bin/ip link set dev %i down

[Install]
WantedBy=multi-user.target

然后我 enable 它但每次启动计算机时都会收到以下错误:

[abc@arch ~]$ systemctl --failed
  UNIT                            LOAD   ACTIVE SUB    DESCRIPTION
● network-wireless@wlp3s0.service loaded failed failed Wireless network connectivity (wlp3s0)

但是,如果我在启动后手动启动此服务:

systemctl start network-wireless@xlp3s0

服务按预期启动。

这是 wpa_supplicant.conf 的内容

ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=wheel
network=
        ssid="TeliaGateway30-91-8F-1C-B2-29"
        #psk="A80871A90A"
        psk=b4d8a1e9ad665eed0178fea6f141134e795e15183a661848b371a41bb73a6844

为什么此服务在手动启动时启动正常,但在启动时无法启动?如何将其更改为在启动时启动?

编辑:添加错误输出: 这就是我得到的错误:

[abc@arch ~]$ journalctl -b -u network-wireless@wlp3s0.service
-- Logs begin at Sat 2015-08-22 12:50:42 CEST, end at Sun 2015-08-23 22:15:26 CEST. --
Aug 23 21:23:36 arch systemd[1]: Starting Wireless network connectivity (wlp3s0)...
Aug 23 21:23:36 arch ip[274]: Cannot find device "wlp3s0"
Aug 23 21:23:36 arch systemd[1]: network-wireless@wlp3s0.service: Main process exited, code=exited, status=1/FAILURE
Aug 23 21:23:36 arch systemd[1]: Failed to start Wireless network connectivity (wlp3s0).
Aug 23 21:23:37 arch systemd[1]: network-wireless@wlp3s0.service: Unit entered failed state.
Aug 23 21:23:37 arch systemd[1]: network-wireless@wlp3s0.service: Failed with result 'exit-code'.
Aug 23 21:25:11 arch systemd[1]: Starting Wireless network connectivity (wlp3s0)...
Aug 23 21:25:11 arch dhcpcd[424]: wlp3s0: waiting for carrier
Aug 23 21:25:16 arch dhcpcd[424]: wlp3s0: carrier acquired
Aug 23 21:25:16 arch dhcpcd[424]: DUID 00:01:00:01:1d:6b:6b:e6:10:0d:7f:b7:30:f3
Aug 23 21:25:16 arch dhcpcd[424]: wlp3s0: IAID c1:c4:73:e0
Aug 23 21:25:16 arch dhcpcd[424]: wlp3s0: soliciting an IPv6 router
Aug 23 21:25:16 arch dhcpcd[424]: wlp3s0: rebinding lease of 192.168.1.85
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: leased 192.168.1.85 for 3600 seconds
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: adding route to 192.168.1.0/24
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: adding default route via 192.168.1.1
Aug 23 21:25:21 arch dhcpcd[424]: forked to background, child pid 477
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: waiting for carrier
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: carrier acquired
Aug 23 21:25:21 arch dhcpcd[424]: DUID 00:01:00:01:1d:6b:6b:e6:10:0d:7f:b7:30:f3
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: IAID c1:c4:73:e0
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: soliciting an IPv6 router
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: rebinding lease of 192.168.1.85
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: leased 192.168.1.85 for 3600 seconds
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: adding route to 192.168.1.0/24
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: adding default route via 192.168.1.1
Aug 23 21:25:21 arch dhcpcd[424]: forked to background, child pid 477
Aug 23 21:25:21 arch systemd[1]: Started Wireless network connectivity (wlp3s0).
Aug 23 21:25:28 arch dhcpcd[477]: wlp3s0: no IPv6 Routers available
Aug 23 22:15:09 arch dhcpcd[477]: wlp3s0: carrier lost
Aug 23 22:15:09 arch dhcpcd[477]: wlp3s0: deleting route to 192.168.1.0/24
Aug 23 22:15:09 arch dhcpcd[477]: wlp3s0: deleting default route via 192.168.1.1
Aug 23 22:15:13 arch dhcpcd[477]: wlp3s0: carrier acquired
Aug 23 22:15:14 arch dhcpcd[477]: wlp3s0: IAID c1:c4:73:e0
Aug 23 22:15:14 arch dhcpcd[477]: wlp3s0: soliciting an IPv6 router
Aug 23 22:15:14 arch dhcpcd[477]: wlp3s0: rebinding lease of 192.168.1.85
Aug 23 22:15:19 arch dhcpcd[477]: wlp3s0: leased 192.168.1.85 for 3600 seconds
Aug 23 22:15:19 arch dhcpcd[477]: wlp3s0: adding route to 192.168.1.0/24
Aug 23 22:15:19 arch dhcpcd[477]: wlp3s0: adding default route via 192.168.1.1
Aug 23 22:15:19 arch dhcpcd[477]: wlp3s0: removing route to 192.168.1.0/24
Aug 23 22:15:26 arch dhcpcd[477]: wlp3s0: no IPv6 Routers available

编辑: 我发现了一个潜在的错误,似乎网络接口在启动过程中从 wlan0 更改了名称,但是我尝试使用 wlan0 启动服务,但结果没有变化。

【问题讨论】:

systemd 会为服务报告哪些错误? 如何检查? 尝试运行:systemctl status network-wireless@wlp3s0.servicejournalctl -b 或者更好,journalctl -b -u network-wireless@wlp3s0.service 【参考方案1】:

服务在启动时停止的原因是因为ip 命令找不到接口。根据systemd services 的手册页,这足以导致服务失败。

正如您自己注意到的那样,界面已重命名或在启动时尚未准备好。

可以查看是否需要添加After=语句 您可以使用systemd-analyze 命令检查启动时的顺序是否正确 您可以拆分服务并使其更加健壮。即使界面尚未准备好,大多数守护程序也可以正常启动。

我个人会让 dhcpd 和 wpa_supplicant 分开服务,并使用 systemd 的 networkd 或 udev 规则来调出接口(如果需要的话)。网上有很多wpa_supplicantdhcpd的单元文件例子,大家看看吧?

【讨论】:

以上是关于无法启动 systemd 服务的主要内容,如果未能解决你的问题,请参考以下文章

systemd 服务无法连接到会话 d-bus

dmjob服务无法启动

为啥centos7的service命令无法启动服务

systemd 开机无法启动privoxy

Docker 容器在启用 systemd 脚本后重新启动后无法启动

Linux systemd 自启动脚本无法正常运行(求助分析方向)