简体   繁体   English

在python中的sudo renice

[英]sudo renice in python

Generically, this is a well answered question. 一般来说,这是一个很好的答案。 Linux doesn't allow non-privileged users to lower a PID's niceness, and running things as root is its own can of worms. Linux不允许非特权用户降低PID的优点,而以root身份运行是他们自己的蠕虫。

That said, here are my specifics: I've got a user account that manages a few processes which has passwordless sudo privileges for renice and a few other commands it uses. 也就是说,这是我的具体内容:我有一个用户帐户管理一些进程,这些进程具有renice密码sudo权限以及它使用的一些其他命令。 I also have a script that is the common entry point for all users on this system. 我还有一个脚本,是该系统上所有用户的通用入口点。 This script can both run regular user programs as well as the processes managed by the special account. 此脚本既可以运行常规用户程序,也可以运行特殊帐户管理的进程。 So the script, when run with a specific option, should renice if it can, but fail silently at the if it cannot. 因此,当使用特定选项运行脚本时,如果可以,则应该进行重renice ,但如果不能则自动失败。

The code I've got for this looks like: 我得到的代码如下:

subprocess.Popen(["sudo", "renice", "-20", str(process.pid)],
#                shell = True,
                 stdout = subprocess.DEVNULL,
                 stderr = subprocess.STDOUT)

If I have shell = True commented out, the process gets its new niceness, but if I'm running as an unprivileged user, sudo kicks out its password prompt and wrecks my terminal output. 如果我已经注释掉了shell = True ,那么这个过程会获得新的好处,但是如果我作为一个非特权用户运行,那么sudo启动它的密码提示并破坏我的终端输出。 Keystrokes become invisible and everything gets stupid looking. 击键变得不可见,一切都变得愚蠢。 If I uncomment shell = True , I get no terminal output. 如果我取消注释shell = True ,我没有终端输出。 However, the process doesn't get its niceness changed, even if I run it as root. 但是,即使我以root身份运行它,该过程也不会改变它的好处。

The corrupted terminal output may well be down to the terminal emulator I'm using (haven't tried it with another one) but I want to merge these behaviors. 损坏的终端输出可能是我正在使用的终端模拟器(没有尝试过另一个)但我想合并这些行为。 Silence from sudo no matter what, but a niceness change if the user can sudo successfully. 无论如何沉默来自sudo ,但如果用户能成功sudo就会改变。

Any pointers? 有什么指针吗?

I think it's because sudo requires a TTY , even when password is not necessary. 我认为这是因为sudo需要TTY ,即使不需要密码也是如此。

Try providing one: 尝试提供一个:

import os
import pty
import subprocess

master, slave = pty.openpty()
p = subprocess.Popen(
    ["sudo", "id", "-a"],
    stdin=slave, stdout=slave, stderr=slave
)
os.close(slave)

output = os.read(master, 1026)
os.close(master)
print(output)

The code above should print something like uid=0(root) gid=0(root) groups=0(root) . 上面的代码应该打印类似uid=0(root) gid=0(root) groups=0(root) If it does, then replace id with renice , remove unnecessary os.read and you should be good. 如果是这样,那么用renice替换id ,删除不必要的os.read ,你应该是好的。

Update: in OP's case, it had failed for another reason. 更新:在OP的情况下,它失败了另一个原因。 Adding start_new_session=True to Popen had it fail silently for unprivileged users and succeed as root. 添加start_new_session=TruePopen了,它静静地失败未经授权的用户和根成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM