Python:使用 setproctitle 更改进程名称 [英] Python: Changing process name with setproctitle

查看:87
本文介绍了Python:使用 setproctitle 更改进程名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启动多个 C++ 程序的 python 脚本,每个程序都传递一个命令行参数,如下所示

I have a python script which launches a number of C++ programs, each program is passed a command line parameter as shown below

process_path "~/test/"
process_name "test"
num_process = 10

for p in range(1, num_processes, 1):
    subprocess.Popen([process_path + process_name, str(p)], shell = False)

我们是否可以通过 setproctitle 重命名这些进程中的每一个,以便我可以将命令行参数作为进程名称的一部分,如果是这样,您会怎么做?

Is it possible to us setproctitle to rename each of these process so that I could include the command line parameter as part of the process name, if so , how would you do it?

推荐答案

setproctitle 只能更改它的自己的"进程标题,因为我认为这是一个安全元素,但是重写进程表的技术是一种古老的 rootkit 技术——很明显这是可能的.

setproctitle can only change it's "own" process title as I would presume a safety element, but the technique of rewriting the process table is an ancient rootkit technique -- so clearly it is possible.

此外,setproctitle 支持多种操作系统,因此您更改进程信息的方法可能会有所不同,但为了便于说明,我假设您在 Linux 下使用它,让我们看看我们有什么.

Furthermore, setproctitle has support for multiple operating systems, so the method in which you change the process information may vary, but for the sake of explanation I'll presume you're using this under Linux and lets see what we have.

Linux 使用 prctl(),看起来您使用的是 prctl(PR_SET_NAME, "my_new_name");,并且这只适用于调用进程.因此,使用 setproctitle 模块似乎没有一种简单"的方法可以做到这一点——您只能修改自己.

Linux uses prctl(), which looks like you use prctl(PR_SET_NAME, "my_new_name");, and this only works on the calling process. So it doesn't look like there's an 'easy' way to do this using the setproctitle module -- you can only modify yourself.

最好的办法是修改 C++ 代码,使其使用 prctl.

Your best bet is to modify your C++ code so that it uses prctl.

如果您不使用 Linux,请发布您使用的内容,因为其他操作系统提供了与 prctl 的限制大不相同的其他机会和方法.

If you're not using Linux, post what you are using as other operating systems provide other opportunities and methods that differ greatly from the limitations of prctl.

这篇关于Python:使用 setproctitle 更改进程名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆