Monday, 30 January 2017

Determine parent process of given process in Linux

Determine Parent Process of given Process in Linux

There is two way to determine
1st Method:

Using ps command will able to determine the parent process ID of given process
Use:
ps -o ppid= <process id>
Example:
ps -o ppid=


Example Shell Script:
#!/bin/sh
pid=$1
if [ -z $pid ]
then
    read -p "PID: " pid
fi
ps -p ${pid:-$$} -o ppid=


2nd Method:

procfs structure given details of PPID of given pid

Use:
cat /proc/<pid of process>/status --> Search PPID

Example Shell Script:
#!/bin/sh
P=$1
if [ -z "$P" ]; then
    read P
fi
cat /proc/"$P"/status | grep PPid: | grep -o "[0-9]*"

No comments:

Post a Comment