Python 3.x Xlib.error.DisplayConnectionError在使用systemd启动操作系统时运行python程序
基本背景资料: 我试图在操作系统启动时运行我的程序(我当前的操作系统是Debian9) 最新稳定版本) 我的项目是听键盘使用Xlib库,我也 使用Tkinter弹出一些UI窗口Python 3.x Xlib.error.DisplayConnectionError在使用systemd启动操作系统时运行python程序,python-3.x,systemd,Python 3.x,Systemd,基本背景资料: 我试图在操作系统启动时运行我的程序(我当前的操作系统是Debian9) 最新稳定版本) 我的项目是听键盘使用Xlib库,我也 使用Tkinter弹出一些UI窗口 这是/etc/system/system/my_project.service 重新启动后,我运行: sudo systemctl status my_project.service 我得到以下错误 May 08 11:49:43 debian keyboard_listener.py[303]:文件 “/usr/
- 重新启动后,我运行:
我得到以下错误sudo systemctl status my_project.service
systemd
无法工作
在没有明确的排序依赖项的情况下,systemd
同时处理事务中的所有单元。这意味着,在启动服务之前,它不会也不会等待X服务器在显示器上显示:0
在现代GNU/Linux发行版中,X服务器不是由任何systemd单元直接启动的,因此不能在X服务器上指定systemd排序依赖项。因此,systemd
不适用于您试图完成的任何任务,至少在systemd中实现更细粒度的依赖机制之前是如此
建议的解决办法
作为一种解决方法,您可以尝试/etc/xdg/autostart
、~/.config/autostart
或~/.xinitrc
(适用于您的X.org设置的内容)。这些脚本保证在图形用户会话中运行
如果需要状态监控,您可以将这两种方法结合起来,使用其中一个脚本或从其中一个脚本内部启动一个瞬态systemd单元。那么,从您的角度来看,什么适合在操作系统启动时运行应用程序(取决于Xlib)?@Guy expanded answer
[Unit]
Description=Daemon tool that opens the required environment given a
certain shortcut key
[Service]
Environment="DISPLAY=:0"
ExecStart=/usr/local/bin/keyboard_listener.py
[Install]
WantedBy=multi-user.target