Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Get all associated pids

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 2 Weeks Ago
SkySmart SkySmart is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 22 January 2017, 2:09 AM EST
Posts: 836
Thanks: 656
Thanked 3 Times in 3 Posts
Get all associated pids

im looking for a portable way to get the PID of the script that is running, and to get every other PIDs that are spawned from it.

and by ever other PIDs, i presume, that would be "child processes".

however, i want to shy away from using any command that is not available on every single unix system thats out there. so the preferred solution would be one that can be used everywhere.

so, here's what im attempting:


Code:
#!/bin/sh
myscriptpid=$$
ps -ef | awk -v myscriptpid="${myscriptpid}" '$2 ~ / $$ / || $3 ~ / $$ / '{print $0}''

can the above be beefed up to spit out the entire line of every single PID that is spawned from my script or related to my script???

Last edited by SkySmart; 2 Weeks Ago at 09:55 PM..
Sponsored Links
    #2  
Old Unix and Linux 2 Weeks Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 23 January 2017, 3:22 AM EST
Location: San Jose, CA, USA
Posts: 9,869
Thanks: 476
Thanked 3,385 Times in 2,891 Posts
Quote:
Originally Posted by SkySmart View Post
im looking for a portable way to get the PID of the script that is running, and to get every other PIDs that are spawned from it.

and by ever other pids, i presume, that would be "child processes".

however, i want to shy away from using any command that is not available on every single unix system thats out there. so the preferred solution would be one that can be used everywhere.

so, here's what im attempting:



Code:
#!/bin/sh
myscriptpid=$$
ps -ef | awk -v myscriptpid="${myscriptpid}" '$2 ~ / $$ / || $3 ~ / $$ / '{print $0}''

can the above been beefed up to spit out every single PID that is spawned or related to my script???
First, single quotes don't work that way in awk or in the shell. Single quotes can't be included in a single-quoted string.

Second, note that / $$ / in awk is a regular expression that will NEVER match anything (if it isn't flagged as an error). It asks awk to look for a <space> character followed by the end of the string being matched followed by the end of the string being matched followed by another <space> character. And, since your field separator in this awkscript is a string of one or more <space> and/or <tab> characters, there can never be a <space> character in the 2nd or 3rd field in your input. But, whether or not there could be a <space> character in a field to be matched, a <space> following the end of the string being matched can NEVER occur.

Third, the default action in awk is {print $0}, so it does not need to be specified.

To fix those problems, change:

Code:
#!/bin/sh
myscriptpid=$$
ps -ef | awk -v myscriptpid="${myscriptpid}" '$2 ~ / $$ / || $3 ~ / $$ / '{print $0}''

to:

Code:
#!/bin/sh
myscriptpid=$$
ps -ef | awk -v myscriptpid="${myscriptpid}" '$2 == myscriptpid || $3 == myscriptpid'

Fourth, note that your definition of "get every other PIDs that are spawned from it" is not at all clear. What you are doing will try to catch the PID of the process and its direct children; it will not catch any offspring of its direct children. If you are looking for all of the processes that have the current process as an ancestor, you would need to do a search like what you already have to find the current process' children and then look for their children recursively until the search for children of a generation of children does not find any more children. But, this does not necessarily find all of your process' offspring. If a child exits without waiting for its children, those children will be inherited by PID #1 (on most systems) and their relationship to your process will be lost.

And, fifth, ps -ef or ps -Af should get you information about all active processes and print their PIDs in field 2 and their parent's PIDs in field 3 on systems that have a ps that conforms to POSIX requirements. But, I can't promise that all systems provide a ps that conforms to POSIX requirements.
The Following User Says Thank You to Don Cragun For This Useful Post:
SkySmart (2 Weeks Ago)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help to kill pids prany_cool Shell Programming and Scripting 4 01-29-2013 11:51 AM
Conflict with PIDs vedder191 Solaris 5 04-26-2010 03:24 PM
differentiating PIDs under 200 prgoodwin Shell Programming and Scripting 2 03-16-2010 03:12 PM
Track and kill the PIDS LRoberts Shell Programming and Scripting 4 09-22-2009 01:41 PM
PIDs of background process ramkumar HP-UX 3 02-09-2005 04:13 PM



All times are GMT -4. The time now is 04:33 AM.