python - 在 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
知道会是什么吗?
解决方案
我按照这篇文章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
推荐阅读
- flutter - 计步器颤振包错误 MissingPluginException(未找到方法在通道 step_detection 上侦听的实现)
- r - “kernelUD 中的错误:至少需要 5 次重定位才能适应主范围”,但每组的重定位数超过 5
- sql-server - 从另一个容器 spring boot 连接到 docker 容器中的 mysql
- javascript - Chrome“无法拖动”图标干扰了鼠标悬停事件,我该如何防止这种情况?
- python - 过滤行数据并将其相加,看看它是否可以通过 reduce() 结合 filter() 给出结果
- c# - 在 Windows Container 中安装字体的正确方法是什么?
- automation - 是否有从厨师检查输出中删除 \n 的语法
- javascript - 如何在JS中创建一个2秒的暂停然后执行一行?
- java - 创建自定义来电屏幕 Android 5.1.1
- android - 如果 Edittext 为空,如何禁用按钮并将 Edittext 输入传递给 android studio 中的另一个活动?