1847725 Members
4304 Online
110265 Solutions
New Discussion

Process names

 
Greg Hitchcock
Occasional Contributor

Process names

Hi

Can anyone help?

I need to enable a script (call it Script2) to find out the Name of the calling script (eg Script1).

I have an idea that the pid or ppid should help but do not know exactly how.

Any suggestions would be welcomed.

Greg Hitchcock
4 REPLIES 4
Robin Wakefield
Honored Contributor

Re: Process names

Hi Greg,

How about:

ps -fp $$ | tail -1 | awk '{print $3}' | read PPID
ps -p $PPID | tail -1 | awk '{print $4}'

For a ksh, you don't need the 1st line, since there is a PPID variable created automatically, containing the parent process id.

Rgds, Robin.
John Palmer
Honored Contributor

Re: Process names

Hi,

Korn/Posix shells will have the variable ${PPID} set to the parent process.

Given that, you can use ps to find the command and arguments of the parent process. Using the UNIX95 options of ps make it easier so to get the parent's command for instance:-

COMM=$(UNIX95= ps -p ${PPID} -o comm |tail -1)

Regards,
John
Suhas_3
Advisor

Re: Process names

Generally, it will be difficult to do so.

If there is a shell script script1 and you execute the script, the output of "ps -f" will give process name as "-ksh" or "sh", and not as "script1" as expected. So you can get the parent process-id, but in many cases it will be difficult to get the process name.

If the script "script1" is executed as "/bin/sh script1" (or similarly ksh or csh etc.) then "ps -f" will show the script name in last column. Then you can either use PPID variable or "$$" to get parent process-id and it's name.


Wodisch
Honored Contributor

Re: Process names

Hello Greg,

how about a one-liner:

parent=$(UNIX95= ps -o comm -p $PPID)

HTH,
Wodisch