Operating System - HP-UX
1748092 Members
5667 Online
108758 Solutions
New Discussion юеВ

Re: how to get PID of father process

 
SOLVED
Go to 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
11 REPLIES 11
Mel Burslan
Honored Contributor

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...
James R. Ferguson
Acclaimed Contributor

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...

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!
Steven E. Protter
Exalted Contributor

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
James R. Ferguson
Acclaimed Contributor

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...

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!
James R. Ferguson
Acclaimed Contributor

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...
James R. Ferguson
Acclaimed Contributor

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...
Hemmetter
Esteemed Contributor

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