在ubuntu 18.04上将python GUI应用程序作为服务运行

时间:2020-05-18 23:21:12

标签: python ubuntu service systemctl

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

[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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我通过遵循这篇帖子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” 环境= XAUTHORITY = / home /“您的用户名” /.Xauthority