linux 中来自其 pid 的进程名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15545341/
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-08-06 22:29:47  来源:igfitidea点击:

Process name from its pid in linux

clinuxprocesssystempid

提问by TheForbidden

How to get a process name from his pid ? For example I execute cat file1.txt, but I want to figure out that cat command and its arguments since its pid in the system. Is there a struct to determine it or something similar? Any idea?

如何从他的 pid 中获取进程名称?例如,我执行 cat file1.txt,但我想弄清楚 cat 命令及其参数,因为它在系统中的 pid。是否有一个结构来确定它或类似的东西?任何的想法?

采纳答案by Anubhab

There is not any general way to do this unix.
Each OS has different ways to handle it and some are very hard. You mention Linux though. With Linux, the info is in the /proc filesystem.
To get the command line for process id 9999, read the file /proc/9999/cmdline.

没有任何通用的方法来做这个unix。
每个操作系统都有不同的处理方式,有些非常困难。不过你提到了Linux。对于 Linux,信息位于 /proc 文件系统中。
要获取进程 ID 9999 的命令行,请读取文件/proc/9999/cmdline.

回答by robbie_c

On linux, you can look in /proc/. Try typing man procfor more information. The contents of /proc/$PID/cmdlinewill give you the command line that process $PIDwas run with. There is also /proc/selffor examining yourself :)

在 linux 上,您可以查看/proc/. 尝试键入man proc以获取更多信息。的内容/proc/$PID/cmdline将为您提供$PID运行进程的命令行。还有/proc/self用于检查自己:)

An alternative (e.g. on Mac OS X) is to use libproc. See libproc.h.

另一种选择(例如在 Mac OS X 上)是使用libproc. 请参阅libproc.h

回答by QJGui

POSIX C does NOT support give a standard API for getting the process name by PID.

POSIX C 不支持提供通过 PID 获取进程名称的标准 API。

In linux, you can get the name by LINUX Proc API: /proc/$PID/cmdline. And the code looks like these:

在 linux 中,您可以通过 LINUX Proc API 获取名称:/proc/$PID/cmdline。代码如下所示:

const char* get_process_name_by_pid(const int pid)
{
    char* name = (char*)calloc(1024,sizeof(char));
    if(name){
        sprintf(name, "/proc/%d/cmdline",pid);
        FILE* f = fopen(name,"r");
        if(f){
            size_t size;
            size = fread(name, sizeof(char), 1024, f);
            if(size>0){
                if('\n'==name[size-1])
                    name[size-1]='
ps -p 9000 -o comm=
'; } fclose(f); } } return name; }

回答by Thunder

To get the process name of a process id say 9000 use this command:

要获取进程 ID 的进程名称,例如 9000,请使用以下命令:

ls -l /proc/[pid]/exe

回答by toddwz

While this question has been answered, I'd like to add my 2 cents.

虽然这个问题已经得到回答,但我想补充我的 2 美分。

In my case, when process 1111creates process 22222via pipe(at least this is what I heard), /proc/2222/cmdlinedoes not give correct process name, but instead gives something like 1111_1. I have to use /proc/2222/commto get the correct process name.

在我的情况下,当进程通过(至少这是我听到的)1111创建进程时,没有给出正确的进程名称,而是给出类似. 我必须使用来获取正确的进程名称。22222pipe/proc/2222/cmdline1111_1/proc/2222/comm

回答by Vikram B

Use the below command in Linux

在 Linux 中使用以下命令

##代码##

It will give the name of the process/application name

它将给出进程/应用程序名称的名称

回答by inihsrah

ps --pid <pid> -o comm h: This command gives executable file name. For example if you run a script name.sh, then the above command gives output as bash

ps --pid <pid> -o comm h: 这个命令给出了可执行文件名。例如,如果您运行脚本 name.sh,则上述命令将输出为bash

ps --ppid <pid> -o comm h: This command gives the output as name

ps --ppid <pid> -o comm h:此命令给出的输出为 name