我使用 Python 开发了一个简单的基于 dbus 的插件 (dbus.service.Object),它从命令行脚本获取 IPC 调用。 dbus 服务配置为托管在 systemd 中。它正在按预期运行。但是,当我在插件中添加启动 gnome 终端的功能时,它失败了。看起来执行被困在任何这些子进程调用中,无论是 call() 还是 Popen()。想知道如何使插件正确启动 gnome-terminal ?
#subprocess.call(['gnome-terminal', '--', '/bin/bash', '-c', 'ls', '-al', '&'])
#subprocess.call(['systemd-run', '--user', '--service-type=forking', 'gnome-terminal', '-t', "Test",'--','/bin/bash', '-c', 'ls', '-al', "&"])
#subprocess.call(['DISPLAY=:0', 'gnome-terminal', '--', '/bin/bash', '-c', 'ls', '-al', '&'])
我还尝试从插件启动 python 脚本,并使用脚本启动 gnome-terminal 但仍然失败。
失败表示终端打不开,ps不显示终端。我也尝试过 os.system。如果直接从控制台运行它就可以工作。
os.system('gnome-terminal --/bin/bash -c "python/data/scripts/test.a.1.py -f/data/station_profile_A.json"')
日志日志显示以下错误:
12 月 21 日 09:34:20 ubuntu 协调器 [5380]:无法初始化服务器:无法连接:连接被拒绝 12 月 21 日 09:34:20 ubuntu coordinator[5380]: # 无法解析参数:无法打开显示:
如果我在 gnome-terminal 前面添加“DISPLAY=:0”,则没有帮助。
最佳答案
要连接到 X,您需要设置 DISPLAY
和 XAUTHORITY
。
例如XAUTHORITY=$HOME/.Xauthority
和DISPLAY=:0.0
。
由XAUTHORITY
命名的文件必须可读。该文件的模式为0600。
我建议您的 DBUS API 包括将 magic cookie 的副本(XAUTHORITY
文件的内容)移交给服务,然后该服务会将其写入临时文件,然后设置 DISPLAY
和 XAUTHORITY
将运行该命令。
关于python - 如何使用 Python 从 dbus 插件(一个 systemd 服务)启动 gnome-terminal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53874944/