python 更改python脚本的进程名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2255444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:11:30  来源:igfitidea点击:

changing the process name of a python script

pythonlinux

提问by jldupont

Is there a way to change the name of a process running a python script on Linux?

有没有办法更改在 Linux 上运行 python 脚本的进程的名称?

When I do a ps, all I get are "python" process names.

当我执行 a 时ps,我得到的只是“python”进程名称。

采纳答案by mechanical_meat

http://code.google.com/p/procname/

http://code.google.com/p/procname/

Sample usage:

示例用法:

# Lets rename:    
>>> procname.setprocname('My super name')    

# Lets check. Press Ctrl+Z       
user@comp:~/procname$ ps

    PID TTY TIME CMD 

13016 pts/2 00:00:00 bash

13128 pts/2 00:00:00 My super name <-- it's here

It will only work on systems where prctlsystem call is present and supports PR_SET_NAMEcommand.

它仅适用于存在prctl系统调用并支持PR_SET_NAME命令的系统。

回答by Sasha Ru

There is simplier (you don't need import any libs) but maybe not so elegant way. You have to do not use "env" inside the shebang line.

有更简单的(你不需要导入任何库)但可能不是那么优雅的方式。您必须不要在 shebang 行内使用“env”。

In other words, this will be named as "python" in process list:

换句话说,这将在进程列表中命名为“python”:

#!/usr/bin/env python

But this will be named with your scriptname:

但这将以您的脚本名称命名:

#!/usr/bin/python

So you'll be able to find it with something like pidof -x scriptnameor ps -C scriptname

所以你可以用类似pidof -x scriptnameps -C scriptname

回答by Matt Seymour

There is the option of doing the following, though it only works on linux (with the prctl(2)call)

可以选择执行以下操作,但它仅适用于 linux(使用prctl(2)调用)

if sys.platform == 'linux2':
    import ctypes
    libc = ctypes.cdll.LoadLibrary('libc.so.6')
    libc.prctl(15, 'My Simple App', 0, 0, 0)

回答by amwinter

the procname library didn't work for me on ubuntu. I went with setproctitle instead (pip install setproctitle). This is what gunicorn uses and it worked for me.

procname 库在 ubuntu 上对我不起作用。我改为使用 setproctitle ( pip install setproctitle)。这就是 gunicorn 使用的,它对我有用。