【发布时间】:2019-03-10 10:25:44
【问题描述】:
我在/home/jack/Code/Service/main.py 中有以下 Python 脚本:
from subprocess import Popen
Popen(["/usr/bin/gedit"])
while True:
pass
我正在使用这个 systemd 服务运行它:
[Unit]
Description=Test
[Service]
Type=simple
Restart=no
User=jack
WorkingDirectory=/home/jack/Code/Service
ExecStart=/usr/bin/python3 main.py
[Install]
WantedBy=multi-user.target
当我使用python3 main.py 从命令行运行main.py 时,它会运行并打开gedit。当我启动 systemd 服务并查看日志时,我看到:
mars 10 11:19:55 Ubuntu-Tower systemd[1]: Started Test.
mars 10 11:19:55 Ubuntu-Tower python3[8827]: Unable to init server: Could not connect: Connection refused
mars 10 11:19:55 Ubuntu-Tower gedit[8835]: cannot open display:
请注意,gedit 进程实际上确实在服务运行时出现在系统监视器中(并且仅在我停止服务时消失),所以这似乎是一个特定于 GUI 的问题。
发生了什么,我怎样才能得到我想要的行为?
【问题讨论】:
-
注意:这似乎是与 this 不同的问题,因为我看不出 PATH 或 PYTHONPATH 问题与此处有何关联。
-
我相信你必须使用
After=graphical.target之类的东西 -
@hansolo 似乎不起作用。我将
After=graphical.target卡在 [Unit] 部分,但我得到了相同的行为。