【问题标题】:How to launch gnome-terminal from dbus plugin, a systemd service, using Python如何使用 Python 从 dbus 插件(一个 systemd 服务)启动 gnome-terminal
【发布时间】:2018-12-20 19:32:36
【问题描述】:

我使用 Python 开发了一个简单的基于 dbus 的插件 (dbus.service.Object),它从命令行脚本接受 IPC 调用。 dbus 服务配置为在 systemd 中托管。它按预期运行。但是,当我在插件中添加功能以启动 gnome-terminal 时,它失败了。无论是 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 但仍然失败。

Fail 表示终端没有打开,ps 没有显示终端。我也尝试过使用 os.system。如果直接从控制台运行,它就可以工作。

os.system('gnome-terminal -- /bin/bash -c "p​​ython /data/scripts/test.a.1.py -f /data/station_profile_A.json"')

日志日志显示以下错误:

12 月 21 日 09:34:20 ubuntu 协调器 [5380]:无法初始化服务器:无法连接:连接被拒绝 Dec 21 09:34:20 ubuntu coordinator[5380]: # Failed to parse arguments: Cannot open display:

如果我在 gnome-terminal 前面添加 'DISPLAY=:0',它没有帮助。

【问题讨论】:

  • “失败”是什么意思?你有例外吗?子进程的 stdout/stderr 是什么? call() 调用是否返回并为您提供退出状态?如果你从一个普通的 Python 程序(不是你的 D-Bus 服务)运行这些调用中的任何一个会发生什么?
  • 我用更多信息更新了这个问题。谢谢!

标签: python systemd dbus gnome-terminal


【解决方案1】:

要连接到 X,您需要同时设置 DISPLAYXAUTHORITY

例如XAUTHORITY=$HOME/.XauthorityDISPLAY=:0.0

XAUTHORITY 命名的文件必须是可读的。该文件是模式 0600。

我建议您让您的 DBUS API 包括将魔术 cookie 的副本(XAUTHORITY 文件的内容)移交给服务,然后该服务会将其写入临时文件,然后使用 DISPLAYXAUTHORITY set 将运行该命令。

【讨论】:

    猜你喜欢
    • 2017-06-24
    • 2015-07-15
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多