语言支持报错
在图形桌面或命令行打开语言支持报错:dbus.exceptions.DBusException: org.freedesktop.DBus.Error.FileNotFound: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
itboone@localhost:/$ sudo /usr/bin/gnome-language-selector
Traceback (most recent call last):
File "/usr/bin/gnome-language-selector", line 21, in on_activate
instance = GtkLanguageSelector(datadir=options.datadir,
File "/usr/lib/python3/dist-packages/LanguageSelector/gtk/GtkLanguageSelector.py", line 185, in __init__
self.ac = aptdaemon.client.AptClient()
File "/usr/lib/python3/dist-packages/aptdaemon/client.py", line 1056, in __init__
self.bus = dbus.SystemBus()
File "/usr/lib/python3/dist-packages/dbus/_dbus.py", line 195, in __new__
return Bus.__new__(cls, Bus.TYPE_SYSTEM, mainloop=mainloop,
File "/usr/lib/python3/dist-packages/dbus/_dbus.py", line 102, in __new__
bus = BusConnection.__new__(subclass, bus_type, mainloop=mainloop)
File "/usr/lib/python3/dist-packages/dbus/bus.py", line 124, in __new__
bus = cls._new_for_bus(address_or_type, mainloop=mainloop)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.FileNotFound: Failed to connect to socket /run/dbus/system_bus_socket: 没有那个文件或目录
一番搜索找到原因:缺少的文件是由dbus daemon服务创建的,但是这个服务没有启动。
手动启动dbus服务后发现:多一个dbus-daemon --system的服务:
itboone@localhost:/run$ ps -ef |grep dbus
itboone 96 8 0 16:31 ? 00:00:00 /usr/bin/dbus-launch --sh-syntax --exit-with-session xfce4-session
itboone 97 8 0 16:31 ? 00:00:00 /usr/bin/dbus-daemon --syslog --fork --print-pid 5 --print-address 7 --session
itboone 108 103 0 16:31 ? 00:00:00 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 11 --address=unix:path=/home/itboone/.cache/at-spi/bus_14.0
itboone 371 9 0 16:33 pts/0 00:00:00 grep --color=auto dbus
itboone@localhost:/run$ sudo /etc/init.d/dbus start
* Starting system message bus dbus [ OK ]
itboone@localhost:/run$ ps -ef |grep dbus
itboone 96 8 0 16:31 ? 00:00:00 /usr/bin/dbus-launch --sh-syntax --exit-with-session xfce4-session
itboone 97 8 0 16:31 ? 00:00:00 /usr/bin/dbus-daemon --syslog --fork --print-pid 5 --print-address 7 --session
itboone 108 103 0 16:31 ? 00:00:00 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 11 --address=unix:path=/home/itboone/.cache/at-spi/bus_14.0
message+ 391 8 0 16:34 ? 00:00:00 /usr/bin/dbus-daemon --system
itboone 976 9 0 16:34 pts/0 00:00:00 grep --color=auto dbus
查看/run/dbus/目录发现文件system_bus_socket已创建:
itboone@localhost:/run$ cd /run/dbus
itboone@localhost:/run/dbus$ ls
pid system_bus_socket
这时命令行启动语言支持
sudo /usr/bin/gnome-language-selector
发现可以正常打开
![](https://img8.php1.cn/3cdc5/1850a/5a0/4acafd80a284b81e.png)
解决方法
dbus服务不能自启的原因尚未找到,但是可以使用笨方法解决,即加入开机启动服务。
通常在Linux中开机启动可以通过
- 编辑/etc/rc.loacl
- 在/etc/init.d/ 下添加启动脚本
- 配置systemd
但这几种方式在子系统中无法使用,我们可以通过 Windows 间接的启动子系统中的服务。
创建wsl启动脚本
进入任意 WSL 发行版中,创建并编辑文件:/etc/init.wsl
sudo vim /etc/init.wsl
脚本内容如下
#! /bin/sh
/etc/init.d/dbus start
设置权限为可执行
sudo chmod +x /etc/init.wsl
这时候可以通过下面的命令来启停设置的服务
sudo /etc/init.wsl [start|stop|restart]
创建windows开机启动脚本
在 Windows 使用快捷键 WIN+R 打开运行窗口,输入 shell:startup 创建启动脚本 linux-start.vbs
Set ws = WScript.CreateObject("WScript.Shell")
ws.run "wsl -d ubuntu2204 -u root /etc/init.wsl start", vbhide
wsl参数说明
-u root 用于指定 wsl 的登录用户为 root
-d ubuntu2204 用于指定 wsl 的发行版
注:-d 后面的发行版名称一定要替换为自己安装的版本(可通过 wsl -l 查看)
重启 Windows 登录账户后就会执行linux-start.vbs,通过VBS执行 init.wsl 中的服务。
wsl -l -v
sudo service --status-all
WSL 中有很多有用的服务,你可以按需删改 /etc/init.wsl ,但没必要塞很多东西进去影响你的启动速度,比如 mysql/mongodb 这些重度服务,可以需要的时候再启动,用完就停了。
参考资料
WSL 服务自动启动的正确方法
https://zhuanlan.zhihu.com/p/47733615
Windows中的WSL2(子系统)开机启动配置
https://blog.csdn.net/hiqiming/article/details/105244147
WSL 的基本命令 | Microsoft Learn
https://learn.microsoft.com/zh-cn/windows/wsl/basic-commands?source=recommendations
手动启动dbus以及dbus daemon
https://blog.csdn.net/bw_yyziq/article/details/83505596
Failed to connect to socket /opt/local/var/run/dbus/system_bus_socket
https://9to5answer.com/failed-to-connect-to-socket-opt-local-var-run-dbus-system_bus_socket-no-such-file-or-directory