Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get child process from parent process

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

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:

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

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

like image 864
AlwaysALearner Avatar asked Jul 19 '13 10:07

AlwaysALearner


People also ask

How do you make a child process from the parent process?

A child process is created as its parent process's copy and inherits most of its attributes. If a child process has no parent process, it was created directly by the kernel. If a child process exits or is interrupted, then a SIGCHLD signal is send to the parent process.

How do you find the processes of a child process?

Using the /proc File System It contains information about the kernel, system, and processes. We can find the PIDs of the child processes of a parent process in the children files located in the /proc/[pid]/task/[tid] directories.

How do kids get PID from Forks?

fork already returns the child's pid. Just store the return value. Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process.

How do I find the process of a Ppid?

How to get a parent PID (PPID) from a child's process ID (PID) using the command-line. e.g. ps -o ppid= 2072 returns 2061 , which you can easily use in a script etc. ps -o ppid= -C foo gives the PPID of process with command foo . You can also use the old fashioned ps | grep : ps -eo ppid,comm | grep '[f]oo' .


2 Answers

Just use :

pgrep -P $your_process1_pid 
like image 121
Miklos Aubert Avatar answered Sep 19 '22 22:09

Miklos Aubert


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

ps --ppid <pid of the parent> 
like image 20
Kent Avatar answered Sep 21 '22 22:09

Kent