如何更改进程名?

时间:2021-12-18 16:25:32

Does anyone know how to change the process name in top?

有没有人知道如何更改上面的进程名?

top - 05:02:47 up 182 days, 10:38,  1 user,  load average: 14.53, 13.11, 11.95
Tasks:   4 total,   2 running,   2 sleeping,   0 stopped,   0 zombie
Cpu(s): 57.2%us, 14.8%sy,  0.0%ni, 26.2%id,  1.3%wa,  0.0%hi,  0.5%si,  0.0%st
Mem:  24736852k total, 22519688k used,  2217164k free,   132268k buffers
Swap:  8386552k total,   741900k used,  7644652k free, 12416224k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                        
 6230 user  20   0 47540 6856 1164 R 41.5  0.0   0:03.10 perl                                                                                                                                       
 6430 user  20   0 14900 1156  936 R  0.3  0.0   0:00.02 top                                                                                                                                        
 6227 user  20   0 47276 7552 2088 S  0.0  0.0   0:00.07 perl                                                                                                                                       
14577 user  20   0 11588 1808 1340 S  0.0  0.0   0:00.46 bash   

I have figured out how to change the top -c name $0 = 'new name.'; However this doesn't accomplish my goal.

我已经知道如何改变-c名称$0 = 'new name.';然而,这并不能实现我的目标。

I found a non standard module, and it looks very promising, However I can't use any non standard modules.

我找到了一个非标准模块,它看起来很有前途,但是我不能使用任何非标准模块。

http://metacpan.org/pod/Sys::Prctl

通过http://metacpan.org/pod/Sys:Prctl

# instead of "perl helloworld.pl"
$0 = "helloworld"
prctl_name("helloworld");

I was hoping someone had some input, or knowledge on changing the title/name of a process.

我希望有人对更改流程的标题/名称有所了解。

I feel I have gone through perlvar pretty thoroughly however I may have missed a simple $^0. Hoping it's that simple.

我感觉我经历了perlvar相当彻底的但是我可能错过了一个简单的$ ^ 0。希望就这么简单。

Edit

@user2783897, not sure why I didn't think of that, here is the basic example I made.

@user2783897,不知道为什么我没有想到这个,这是我做的一个基本的例子。

sub prctl_name {
    my $TASK_COMM_LEN = 16;
    my $SYS_prctl = 157;
    my $SYS_PR_SET_NAME = 15;
    my $SYS_PR_GET_NAME = 16;

    my ($str) = @_;

    if(defined $str) {
        my $rv = prctl($SYS_PR_SET_NAME, $str);
        if($rv == 0) {
            return 1;
        } else {
            return;
        }

    } else {
        $str = "\x00" x ($TASK_COMM_LEN + 1); # allocate $str
        my $ptr = unpack( 'L', pack( 'P', $str ) );
        my $rv = prctl($SYS_PR_GET_NAME, $ptr);
        if($rv == 0) {
            return substr($str, 0, index($str, "\x00"));
        } else {
            return;
        }
    }
}
sub prctl {
    my $SYS_prctl = 157;
    my ($option, $arg2, $arg3, $arg4, $arg5) = @_;
    syscall($SYS_prctl, $option,
        ($arg2 or 0), ($arg3 or 0), ($arg4 or 0), ($arg5 or 0));
}

1 个解决方案

#1


2  

Why don't you copy Sys/Prctl.pm code inside your own ? It's only a few dozens of lines.

你何不复印系统/Prctl ?你自己的pm代码?只有几十行。

Furthermore, most of the code is dedicated to finding on which kind of kernel the process is running, to select the appropriate SYS_prctl parameter. If you know on what kind of kernel you're running, you can cut the code to its bare bone.

此外,大部分代码都用于查找进程正在运行的内核类型,以选择合适的SYS_prctl参数。如果您知道您正在运行的是哪种内核,那么您可以将代码精简到最低限度。

#1


2  

Why don't you copy Sys/Prctl.pm code inside your own ? It's only a few dozens of lines.

你何不复印系统/Prctl ?你自己的pm代码?只有几十行。

Furthermore, most of the code is dedicated to finding on which kind of kernel the process is running, to select the appropriate SYS_prctl parameter. If you know on what kind of kernel you're running, you can cut the code to its bare bone.

此外,大部分代码都用于查找进程正在运行的内核类型,以选择合适的SYS_prctl参数。如果您知道您正在运行的是哪种内核,那么您可以将代码精简到最低限度。