Get all associated pids


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get all associated pids
# 1  
Old 01-07-2017
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; 01-07-2017 at 09:55 PM..
# 2  
Old 01-07-2017
Quote:
Originally Posted by SkySmart
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.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help to kill pids

Hi there !!! I am writing a script to kill the pids on different linux boxes :cool: the output of my command gives the pids running on that box, but how can I kill all the pids without looping? :confused: Code: ssh $i ps -fu $USER | grep ManServer | grep -v grep | awk '{print $2}' | kill ... (4 Replies)
Discussion started by: prany_cool
4 Replies

2. Solaris

Conflict with PIDs

I am trying to determine the root cause of a java process that dies trying to startup during it's cron job. I did go ahead and change the time that it starts up in the cron file and now it starts successfully. However is there a way to determine what PID a process was attempting to get when... (5 Replies)
Discussion started by: vedder191
5 Replies

3. Shell Programming and Scripting

differentiating PIDs under 200

Hey, So I'm new to shell scripting, and I'm trying to write one for my lab that will keep down the work load by deleting processes that are left over from previous sessions. Basically I want it to do three things. 1) Check the processes running 2) See if that person is logged on. 3) if... (2 Replies)
Discussion started by: prgoodwin
2 Replies

4. Shell Programming and Scripting

Track and kill the PIDS

I have a script that conducts some SSH calls and I would like to capture the child info so that I can do a sleep and then a cleanup to make sure they do not stay out there as ghosts. I was told I could do something like this... #!/bin/sh for m = job1, job2, job3 x=1... (4 Replies)
Discussion started by: LRoberts
4 Replies

5. Shell Programming and Scripting

Comparing PIDs in a Shell...

Hi, There is a file having a list of running PIDs (pid_process) and another file having a list of registered PIDs (pid_regieter). I want to check if:- a) there is at least one running PID that does not correspond to a registered PID (listing the PID not registered in the file) ... (1 Reply)
Discussion started by: marconi
1 Replies

6. Shell Programming and Scripting

comparing PIDs in shell..

Hi, There is a file having a list of running PIDs and another file having a list of registered PIDs. How can we check if the number of running PIDs are less or more than the registered PIDs, comparing the total no. in each and also each value. Request you to pls give your inputs. Thanks a... (2 Replies)
Discussion started by: marconi
2 Replies

7. Shell Programming and Scripting

Shell Script for PIDs

I am trying to write a Shell script wherein the shell needs to read a list of PID in the File $stat/bin/Process and compare it to the PID of the processes running on a server. Also the script should return KO(not OK) with corresponding label :- a) When an environmental variable not... (2 Replies)
Discussion started by: marconi
2 Replies

8. UNIX for Dummies Questions & Answers

How to find the maximum # of PIDs

Is there a command in HP Unix which can be used inside a K shell to find out the maximum number of processes (PIDs) a pc can generate? Any help will be greatly appreciated. Steve (8 Replies)
Discussion started by: stevefox
8 Replies

9. HP-UX

PIDs of background process

How to track the pid of a background child process(shell script) from a parent script ? For example :- $ sleep 10000 & 4220 where 4220 is the pid of bg process sleep. Now my requirement is to keep the sleep statement in a shell script(test.ksh) and will be invoking it from... (3 Replies)
Discussion started by: ramkumar
3 Replies
Login or Register to Ask a Question