Linux 如何从父进程获取子进程

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

How to get child process from parent process

linuxshellprocesschild-process

提问by AlwaysALearner

Is it possible to get the child process id from parent process id in shell script?

是否可以从shell脚本中的父进程ID获取子进程ID?

I have a file to execute using shell script, which leads to a new process process1(parent process). This process1has forked another process process2(child process). Using script, I'm able to get the pid of process1using the command:

我有一个要使用 shell 脚本执行的文件,这会导致一个新进程process1(父进程)。这个进程 1已经派生出另一个进程进程2(子进程)。使用脚本,我可以使用以下命令获取process1的 pid :

cat /path/of/file/to/be/executed

but i'm unable to fetch the pid of the child process.

但我无法获取子进程的 pid。

采纳答案by Miklos Aubert

Just use :

只需使用:

pgrep -P $your_process1_pid

回答by Basile Starynkevitch

The shell process is $$since it is a special parameter

shell进程是$$因为它是一个特殊的参数

On Linux, the proc(5)filesystem gives a lot of information about processes. Perhaps pgrep(1)(which accesses /proc) might help too.

在 Linux 上, proc(5)文件系统提供了大量有关进程的信息。也许 pgrep(1)(访问/proc)也可能有所帮助。

So try cat /proc/$$/statusto get the status of the shell process.

所以尝试cat /proc/$$/status获取shell进程的状态。

Hence, its parent process id could be retrieved with e.g.

因此,它的父进程 id 可以用例如检索

  parpid=$(awk '/PPid:/{print }' /proc/$$/status)

Then use $parpidin your script to refer to the parent process pid (the parent of the shell).

然后$parpid在脚本中使用来引用父进程 pid(shell 的父进程)。

But I don't think you need it!

但我认为你不需要它!

Read some Bash Guide(or with caution advanced bash scripting guide, which has mistakes) and advanced linux programming.

阅读一些Bash 指南(或谨慎的高级 bash 脚本指南,其中有错误)和高级 linux 编程

Notice that some server daemon processes (wich usually need to be unique) are explicitly writing their pid into /var/run, e.g. the ?sshdserver daemon is writing its pid into the textual file /var/run/sshd.pid). You may want to add such a feature into your own server-like programs (coded in C, C++, Ocaml, Go, Rust or some other compiledlanguage).

请注意,某些服务器守护进程(通常需要是唯一的)将其 pid 显式写入/var/run,例如 ? sshd服务器守护程序正在将其 pid 写入文本文件/var/run/sshd.pid)。您可能希望将这样的功能添加到您自己的类似服务器的程序中(用 C、C++、Ocaml、Go、Rust 或其他一些编译语言编码)。

回答by Kent

I am not sure if I understand you correctly, does this help?

我不确定我是否理解正确,这有帮助吗?

ps --ppid <pid of the parent>

回答by Goofy

I'v written a scrpit to get all child process pids of a parent process. Here is the code.Hope it helps.

我写了一个 scrpit 来获取父进程的所有子进程 pid。这是代码。希望它有帮助。

function getcpid() {
    cpids=`pgrep -P |xargs`
#    echo "cpids=$cpids"
    for cpid in $cpids;
    do
        echo "$cpid"
        getcpid $cpid
    done
}

getcpid 

回答by krishna

ps -axf | grep parent_pid 

Above command prints respective processes generated from parent_pid, hope it helps. +++++++++++++++++++++++++++++++++++++++++++

上面的命令打印了从 生成的各个进程parent_pid,希望对您有所帮助。+++++++++++++++++++++++++++++++++++++++++++++

root@root:~/chk_prgrm/lp#

 parent...18685

 child... 18686


root@root:~/chk_prgrm/lp# ps axf | grep frk

 18685 pts/45   R      0:11  |       \_ ./frk

 18686 pts/45   R      0:11  |       |   \_ ./frk

 18688 pts/45   S+     0:00  |       \_ grep frk

回答by user7329527

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    // Create a child process     
    int pid = fork();

    if (pid > 0)
    {

            int j=getpid();

            printf("in parent process %d\n",j);
    }
    // Note that pid is 0 in child process
    // and negative if fork() fails
    else if (pid == 0)
    {





            int i=getppid();
            printf("Before sleep %d\n",i);

            sleep(5);
            int k=getppid();

            printf("in child process %d\n",k);
    }

    return 0;

}

}

回答by where23

To get the child process and thread, pstree -p PID. It also show the hierarchical tree

要获取子进程和线程, pstree -p PID. 它还显示层次树