首页 > 解决方案 > 在 ubuntu 18.04 上运行 python GUI 应用程序作为服务

问题描述

我已经成功地将我的 GUI python 应用程序作为 Raspberry pi 中的服务运行。使用的单元文件:

[Unit]
Description=Example systemd service.
After=graphical.target

[Service]
Type=simple
Environment="Display=:0"
Environment=XAUTHORITY=/home/pi/.Xauthority
WorkingDirectory=/home/pi/tf/
ExecStart=/home/pi/tf/myApp.py
Restart=always
RestartSec=10s
KillMode=process
Timeout=infinity

[Install]
WantedBy=graphical.target

在我的 python 应用程序的开头,我添加了这样的路由 python3:

#! /address/where/is/python3

问题是我不能在 Ubuntu 中做同样的事情。

我认为是因为 .Xauthority 文件不存在。在ubuntu中我跑了

echo $XAUTHORITY

我得到了:

/run/user/1000/Xauthority

然后我改变这些行:

Environment=XAUTHORITY=/run/user/1000/Xauthority
WorkingDirectory=/home/sergio/tf/
ExecStart=/home/sergio/tf/myApp.py

使用“journalctl -u myApp -f”显示以下错误:

cannot connect to X server

知道会是什么吗?

标签: pythonubuntuservicesystemctl

解决方案


我按照这篇文章https://askubuntu.com/questions/21923/how-do-i-create-the-xauthority-file中 htorque 的步骤解决了这个问题

请按照以下步骤操作:

1-打开系统>首选项>启动应用程序

2-单击添加:

3-名称:Xauthority

4-命令:/bin/bash -c 'ln -s -f "$XAUTHORITY" ~/.Xauthority' 注释:创建从 ~/.Xauthority 到 $XAUTHORITY 的符号链接,并通过单击添加添加条目。

现在每次登录时,它都应该创建指向当前授权文件的链接。

所以现在我们有文件 .Xauthority 在 ~/ 最后服务文件 myApp.service 更新如下:

Environment="Display=:0" Environment=XAUTHORITY=/home/"yourUsername"/.Xauthority


推荐阅读