如何从父进程获取子进程

时间:2021-12-28 13:38:52

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 process1 has forked another process process2(child process). Using script, I'm able to get the pid of process1 using 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。

7 个解决方案

#1


88  

Just use :

只使用:

pgrep -P $your_process1_pid

#2


40  

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

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

ps --ppid <pid of the parent>

#3


17  

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 $1|xargs`
#    echo "cpids=$cpids"
    for cpid in $cpids;
    do
        echo "$cpid"
        getcpid $cpid
    done
}

getcpid $1

#4


13  

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/$$/status to 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 $2}' /proc/$$/status)

Then use $parpid in 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  sshd server 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 compiled language).

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

#5


3  

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

#6


0  

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

为了获得子进程和线程,pstree -p PID。它还显示了层次树

#7


-2  

#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;

}

}

#1


88  

Just use :

只使用:

pgrep -P $your_process1_pid

#2


40  

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

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

ps --ppid <pid of the parent>

#3


17  

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 $1|xargs`
#    echo "cpids=$cpids"
    for cpid in $cpids;
    do
        echo "$cpid"
        getcpid $cpid
    done
}

getcpid $1

#4


13  

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/$$/status to 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 $2}' /proc/$$/status)

Then use $parpid in 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  sshd server 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 compiled language).

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

#5


3  

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

#6


0  

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

为了获得子进程和线程,pstree -p PID。它还显示了层次树

#7


-2  

#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;

}

}