Re: how to get PID of father process (964 Views)
Reply
Advisor
Juergen Feldmann
Posts: 40
Registered: ‎03-12-1998
Message 1 of 12 (964 Views)
Accepted Solution

how to get PID of father process

Hi all,

any chance for some help?

I tried the command:
ps -ef | grep -i vdsd | grep -v root
to find PIDs of processes:
vds2 23246 1 0 12:39:32 ? 0:03 vdsd -g
vds2 29284 23246 3 14:12:37 ? 0:00 vdsd -g

The father process forks child processes and sometimes the father needs a restart (will then get new PID). How can I get the PID of the father process´if there are probably 10 child processes?

Any help would be much appreciated.
Thanks in advance,
Juergen
Honored Contributor
Mel Burslan
Posts: 3,214
Registered: ‎08-26-1998
Message 2 of 12 (964 Views)

Re: how to get PID of father process

PID=Process ID
PPID=Parent Process ID (I am assuming this is what you meant by father process"

PID=`ps -ef | grep process_name_string | awk {'print $2'}
PPID=`ps -ef | grep process_name_string | awk {'print $3'}

Hope this helps
________________________________
UNIX because I majored in cryptology...
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 12 (964 Views)

Re: how to get PID of father process

Hi Juergen:

Given a child's pid (for example 2342):

# UNIX95= ps -p 2342 -o ppid=
1

...which indicates that 'init' is the parent process.

Or, if you know the basename of the child process:

# UNIX95= ps -C getty -o ppid=
1

In both cases, set the XPG4 (UNIX95) behavior of 'ps' only for the duration of the commandline by writing 'UNIX95= ps'. Note that no semicolon occurs. If you prefer you could do: 'UNIX95=1 ps' but do _not_ assume that 'uNIX95=0' disarams it.

The UNIX95 behavior for 'ps' allows you to select the fields you want to see with '-o'. The "=" after the field name suppresses the header for the field.

Regards!

...JRF...

Advisor
Juergen Feldmann
Posts: 40
Registered: ‎03-12-1998
Message 4 of 12 (964 Views)

Re: how to get PID of father process

Sorry for my poor English!
Thanks, it helps and it does not, I created a script like that:

# !/bin.sh
PPID=`ps -ef|grep vdsd|grep -v root|grep -v vds2s|awk {'print $3'}`
PID=`ps -ef|grep vdsd|grep -v root|grep -v vds2s|awk {'print $2'}`

if [ $PPID -eq 1 ]
then
echo $PID
fi

Sometimes the script displays a PID and sometimes a syntax error. Hmmmh I'm not sure why it happens sometime!
As I mentioned the parent process ID is the one I'm looking for! And not 2 or more child processes!

Thanks!
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 5 of 12 (964 Views)

Re: how to get PID of father process

Shalom,

UNIX95=1

ps -efH

This will show an indented display showing father processes at the left.

Modify this script to see the PPID field.

http://www.hpux.ws/?p=8

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 12 (964 Views)

Re: how to get PID of father process

Hi (again) Juergen:

Using 'grep' to filter and select the output of 'ps' is a poor way to be sure that you find what (and only what you want).

The UNIX95 behavior allows the '-C' option as I showed which allows you to match _exactly_ the basename of a process. With the UNIX95 mode you can also select only the 'ps' fields you want to return (with the '-o' option) and optionally suppress the header line. THis makes life easy and exacting.

Regards!

...JRF...
Advisor
Juergen Feldmann
Posts: 40
Registered: ‎03-12-1998
Message 7 of 12 (964 Views)

Re: how to get PID of father process

Hi and thanks guys but to be honest, I've never heard of UNIX95, what the heck is that? Any chance for a manual?

I tried this below:
UNIX95= ps -C vdsd -o pid -o ppid
and got the output:
PID PPID
14514 1
14511 1
16364 1

The last PID would be the right one!
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 12 (964 Views)

Re: how to get PID of father process

Hi Juergen:

UNIX95 is the XPG4 standard.

http://en.wikipedia.org/wiki/XPG4#Output

Setting the variable lends different behavior to various commands, perhaps the most notable and useful being 'ps'.

For 'ps' simply see the manpages for more information.

Be extremely careful _not_ to set the behavior in your profile or at some global level in a script. Many commands will have their standard behavior altered in subtle ways.

For this reason, confine the use of this behavior to a command-by-command basis by setting it only on the command line for the duration of the command line as I showed in my original post.

Regards!

...JRF...
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 9 of 12 (964 Views)

Re: how to get PID of father process

Hi (again) Juergen:

Actually another good reference to the XPG4/UNIX95 environment is this:

http://docs.hp.com/en/B2355-60130/standards.5.html

Regards!

...JRF...
Esteemed Contributor
Hemmetter
Posts: 284
Registered: ‎10-17-2000
Message 10 of 12 (964 Views)

Re: how to get PID of father process

Hallo Juergen,

from e.g. ps(1) "EXTERNAL INFLUENCES":

EXTERNAL INFLUENCES
Environment Variables
UNIX95 specifies to use the XPG4 behavior for this command. The changes for XPG4 include support for the entire option set specified above and include (the following) behavioral changes.

rgds
HGH

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 11 of 12 (964 Views)

Re: how to get PID of father process

Hi Juergen:

> Hi and thanks...

When you are satisfied with the information that has been offered, please don't forget to evaulate it by providing feedback:

http://forums13.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
Advisor
Juergen Feldmann
Posts: 40
Registered: ‎03-12-1998
Message 12 of 12 (964 Views)

Re: how to get PID of father process

Thanks guys for your help and support - have to study some "manuals" now.
Regards
Juergen
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.