运行os.system,好像没有用sudo执行python

问题描述 投票:-2回答:1

当我运行以下命令时,everyhting按预期工作。没有错误,我收到一条说“你好”的系统通知:

$ python3
>>> import os
>>> os.system("notify-send Hello")
0

但是,当我这样做时:

$ sudo python3
>>> import os
>>> os.system("notify-send Hello")

脚本卡住了,没有任何反应。

然后我尝试这样做:

$ sudo python3
>>> import os
>>> os.seteuid(1000)
>>> os.system("notify-send Hello")

1000是我的普通非root用户帐户) 但是,脚本仍然卡住而且没有任何反应。

我也试过这个:

$ sudo python3
>>> import os
>>> os.system("su my-user-name -c 'notify-send Hello'")

还有这个:

$ sudo python3
>>> import os
>>> os.seteuid(1000)
>>> os.system("su my-user-name -c 'notify-send Hello'")

他们都有同样的问题......

我不是在寻找另一种创建通知的方法。我对subprocess或像notify2这样的东西不感兴趣,这会在我的系统上引起一系列新问题。哦,请不要告诉我不要使用sudo。我有我的理由。

python python-3.x os.system notify-send
1个回答
3
投票

我通过反复试验发现的实现细节是notify-send需要XDG_RUNTIME_DIR环境变量才能运行 - 至少对于这些版本:

$ dpkg -l | grep libnotify
ii  libnotify-bin                              0.7.7-3                                      amd64        sends desktop notifications to a notification daemon (Utilities)
ii  libnotify4:amd64                           0.7.7-3                                      amd64        sends desktop notifications to a notification daemon

我首先通过使用env -i notify-send hello确定它需要某种环境变量,它没有产生任何通知。

然后我用this script的修改版本将环境一分为二

如何获得该环境变量取决于您,但您需要将notify-send作为正确的用户并使用该变量集运行。

这是一个示例python脚本,由于其安全问题,我拒绝使用os.system

import os
import pwd
import subprocess
import sys


def main():
    if len(sys.argv) != 2:
        raise SystemExit(f'usage `{sys.argv[0]} USER`')
    if os.getuid() != 0:
        raise SystemExit('expected to run as root')

    # find the `gnome-session` executable, we'll use that to grab
    # XDG_RUNTIME_DIR
    cmd = ('pgrep', '-u', sys.argv[1], 'gnome-session')
    pid = int(subprocess.check_output(cmd))

    # read that process's environment
    with open(f'/proc/{pid}/environ') as f:
        for line in f.read().split('\0'):
            if line.startswith('XDG_RUNTIME_DIR='):
                _, _, xdg_runtime_dir = line.partition('=')
                break
        else:
            raise SystemExit('Could not find XDG_RUNTIME_DIR')

    # run the notify send as the right user
    uid = pwd.getpwnam(sys.argv[1]).pw_uid
    os.seteuid(uid)
    os.environ['XDG_RUNTIME_DIR'] = xdg_runtime_dir
    os.execvp('notify-send', ('notify-send', 'ohai'))


if __name__ == '__main__':
    exit(main())

免责声明:这个脚本正在做一些非常hacky的事情,我不一定会在生产代码中提出这些建议。值得注意的是:

  • 炮轰pgrep找到一个进程
  • 读取另一个进程的环境变量
  • sudo

样品用法:

$ python3 t.py
usage `t.py USER`
$ python3 t.py asottile
expected to run as root
$ sudo python3 t.py asottile
# (I get a notification for `ohai`)
© www.soinside.com 2019 - 2024. All rights reserved.