Command to find parent and child process?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Command to find parent and child process?
# 1  
Old 04-21-2011
Command to find parent and child process?

Hi,

I have a script that calls other scripts/commands which may or may not spawn other process.

From my understanding, when I do a ps -ef, the highest numbered process ID is supposed to be the parent ID of all the other related child processes, is this correct?

In most or all circumstances, I do a ps -ef | grep <processid> of my script and anything that spawns off that process IDs I assumed are the child processes of my script. If I want to terminate my script and all other child processes, then I kill the parent ID which is the highest numbered PID and this will subsequently kill all other child process IDs, is this correct?

Now, my question is whether there is any quick way of showing what are the child processes of a parent ID instead of what am currently doing now which is visually checking which one is the parent ID and "assuming" that the highest numbered PID is the parent ID of all the other processes.

Below is a sample output of running ps -ef | grep exp | grep -v grep. I assume from the output below that the parent process/ID is PID 11322, is that correct?

Any advice or suggestion will be much appreciated. Thanks in advance.

Code:
oracle   11154 11153  0 21:20 ?        00:00:00 /bin/sh -c (. ~oracle/.profile; /bin/ksh /usr/local/oracle/scripts/expdp_o
oracle   11155 11154  0 21:20 ?        00:00:00 /bin/sh -c (. ~oracle/.profile; /bin/ksh /usr/local/oracle/scripts/expdp_o
oracle   11190 11155  0 21:20 ?        00:00:00 /bin/ksh /usr/local/oracle/scripts/expdp_schema.ksh dev01
oracle   11322 11190  0 21:20 ?        00:00:00 expdp

# 2  
Old 04-21-2011
You're incorrect in your reasoning, the output shows a clear inheritance tree. For your own enlightenment try the following
Code:
ps -ef | egrep  '^ *(UID|exp)' | grep -v grep

The first line of the ps -ef output lets you know the contents of the column below each title.
Code:
UID        PID  PPID  C STIME TTY          TIME CMD

PPID is the parent process ID , so you could check for your PID in the 3rd column (assuming GNU or SunOS ps here) and report the second column if true.
The following will return all process spawned by init (PID=1)
Code:
ps -ef | awk '{if ($3 == 1) print $2 ": " $8;}'


Last edited by Skrynesaver; 04-21-2011 at 11:19 AM.. Reason: Added awk example
# 3  
Old 04-21-2011
Quote:
From my understanding, when I do a ps -ef, the highest numbered process ID is supposed to be the parent ID of all the other related child processes, is this correct?
No, this is totally wrong.

with your "ps" listing there are two relevant columns:
PID = Process ID of the process
PPID = Process ID of the parent process

It is not unusual for PPID to be less than PID. The simplest example is "init" (PID 1).
Process IDs have a finite maximum number defined in the kernel. On a busy system it is possible for the same PID to be used on the same day. Wise administrators configure the kernel with a large maximum PID.

You don't mention what Operating System you have. Some have the "ptree" command which you may find useful, others have switches to "ps" to generate the tree.
# 4  
Old 04-21-2011
You can also hav a look at

Code:
man ptree

and/or (may depend on your OS)
Code:
man pstree

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How make parent to wait from child process?

Hi all, I am starting mgen5 for sometime depends on input from a file, in a child process. now I want to make parent to wait in this child process till mgen5 finishes, or timeout happens. could anyone please tell me how to make parent to wait in child process in shell script? thanks... (2 Replies)
Discussion started by: girijajoshi
2 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

4. Programming

Parent process starts before the child using signal, in C

Hi, i want that the parent process start before the child, this code doesn't work, if the child start before the parent it wait for signal, then the father send the signal SIGALRM and the child catch it and call printf; else the father call printf and send the signal to the child that call its... (1 Reply)
Discussion started by: blob84
1 Replies

5. Programming

IPC - pipes between parent and child process

Hi guys, I'm having some problem here, I'm studying pipes, and i want to create a shell in C and at this point a don't want to use semaphores, instead I want to use tricks. Straight to the doubt: I've a parent and a child process, and both of them has some code to execute, and the child process... (5 Replies)
Discussion started by: pharaoh
5 Replies

6. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

7. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

8. UNIX for Advanced & Expert Users

How to find all the child processes of a parent process

Hi I am trying to see if there are some options in ps command or if there is a shell script which basically shows you all the processes spawned by a parent process , then all the processes of its child processes and so on down the hierarchy may be like a tree structure. It might be a generic... (6 Replies)
Discussion started by: clifford
6 Replies

9. Programming

display in a child process a command called in the parent one

Hi , Could you tell me if I am right 1. Using fork(), pipe(), execlp() and dup() (see man 2 dup), write a C program executing the command ps -j in a parent process, displaying the result in a child process. #include <unistd.h> #include <errno.h> #include <stdio.h> #include <unistd.h>... (7 Replies)
Discussion started by: remid1985
7 Replies

10. Programming

parent and child process question?

Hi everybody, I'm trying to understand how a parent and child processes interact. This function( below) basically measures the fork time from the perspective of the parent only. what i would like to know is how to measure the time from the perspective of parent and child (ie: inserting... (0 Replies)
Discussion started by: tosa
0 Replies
Login or Register to Ask a Question