LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2007, 08:54 AM   #1
bugmenot
LQ Newbie
 
Registered: Jan 2006
Posts: 12

Rep: Reputation: 0
Find parent PID in shell script ?


Hello,

I know that I can with $$ inside shell script find out the PID of that shell script.

But how can I get parent PID of the same script inside shell script.

Example:

I have MyScript and MyScript1.
MyScript is calling MyScript1.
So if I inside MyScript1 write $$ I will get PID of MyScript1, but how to get PID of MyScript inside MyScropt1.

Is there a way?

Thanks
 
Old 02-07-2007, 09:06 AM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
You can get Parent PID (PPID) using ps. These are the columns available with the -ef options:

Code:
[hector@troy ~]$ ps -ef | head -1
UID        PID  PPID  C STIME TTY          TIME CMD
Once you have your own PID, you can match it with awk and then print the 3rd column.
e.g.
Code:
ps -ef | awk '$2 ~ /\<1148\>/ { print $3; }'
where 1148 is your PID, and the result printed is its PPID.

Last edited by anomie; 02-07-2007 at 09:07 AM.
 
Old 02-07-2007, 09:19 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400
Hi,

I assume you are using bash: You can use PPID.

Code:
$ cat ms
#!/bin/bash

echo "ms => $$"

./ms1

$ cat ms1
#!/bin/bash

echo "-- ms1 => $$"
echo "-- ms => $PPID"


$ ./ms
ms => 24158
-- ms1 => 24159
-- ms => 24158
Hope this helps.
 
1 members found this post helpful.
Old 02-07-2007, 09:29 AM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Even easier -- I didn't know there was a special shell variable for PPID.
 
Old 02-07-2007, 09:32 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400
Hi,

Yep, it's a read-only variable set by bash. See the manpage (part about shell variables) for all the gory details
 
Old 02-07-2007, 09:48 AM   #6
bugmenot
LQ Newbie
 
Registered: Jan 2006
Posts: 12

Original Poster
Rep: Reputation: 0
Ok, good it is working also in "ksh"
 
Old 02-07-2007, 11:53 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,290

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
hi, what is the difference between $$ and $! ?

thanks,
 
Old 02-07-2007, 12:09 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400
Hi,

$$ -> Expands to the process ID of the (current) shell.
$! -> Expands to the process ID of the most recently executed background command.

man bash could have told you this......
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
! HELP ! Script : How to control parent shell ??? fawqati Programming 1 06-03-2006 12:58 PM
get the PID of a separate process (no parent-child relationship) arunj Linux - Software 6 02-03-2006 01:50 AM
obtaining PID of shell script sanjith11 Programming 3 07-14-2004 10:43 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration