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

2024-05-08 20:38:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经成功地将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

知道会是什么吗


Tags: 文件py应用程序targethomeenvironmenttfpi
1条回答
网友
1楼 · 发布于 2024-05-08 20:38:46

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

按照以下步骤操作:

1-Open System > Preferences > Startup Applications

2-Click on Add :

3-Name: Xauthority

4-Command: /bin/bash -c 'ln -s -f "$XAUTHORITY" ~/.Xauthority' Comment: Creates a symbolic link from ~/.Xauthority to $XAUTHORITY and add the entry by clicking on Add.

Now every time you log in, it should create the link to the current authority file.

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

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

相关问题 更多 >