更改python脚本的进程名称[重复]

时间:2021-12-01 02:57:11

This question already has an answer here:

这个问题在这里已有答案:

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.

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

4 个解决方案

#1


16  

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 prctl system call is present and supports PR_SET_NAME command.

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

#2


13  

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 scriptname or ps -C scriptname

所以你可以用像pidof -x scriptname或ps -C scriptname这样的东西找到它

#3


8  

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)

#4


4  

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使用的,它对我有用。

#1


16  

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 prctl system call is present and supports PR_SET_NAME command.

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

#2


13  

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 scriptname or ps -C scriptname

所以你可以用像pidof -x scriptname或ps -C scriptname这样的东西找到它

#3


8  

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)

#4


4  

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使用的,它对我有用。