Finding process id of subsequent process


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Finding process id of subsequent process
# 1  
Old 10-30-2009
Finding process id of subsequent process

hi all,

I am trying to find the process id of the subsequent process created via fork and exec calls in perl.

For eg:
Code:
envVarSetter dataCruncher.exe < input.txt > output.txt

When I fork and exec the above command,
it returns only the pid of envVarSetter and I don't know how to find the pid of dataCruncher.exe

Option of pgrep, grepping by process name don't seem to suit my case and it will only involve additional processing. Also, multiple different parameters are passed which when grepped will return different values, so that's not a nice option

In short, is there a way to find out the pid of subsequent process executed in one command?
# 2  
Old 10-30-2009
What gives the following :
Code:
pgrep -P $$

option of pgrep :
-P ppid,... Only match processes whose parent process ID is listed.

as $$ returns the ID of the current program, the command above should return the ID's of subsequent processes.
# 3  
Old 10-30-2009
Thanks for the reply.
But that is not the case really.

As I have told that, 2 processes are executed in sequence,
where the first one completes [ envVarSetter ] in no time which means the second process [ dataCruncher.exe ] would be left orphaned and init adopts that, therefore PPID of the second process is 1 and no more the PID that created it.

This makes the problem hard to find out the PID of the subsequent process.
Since they almost execute one after another, I have noticed most of the times that if PID of first process is 'x' and PID of second process is 'x+1' but that is a crude assumption and I can't go with that.

In a very busy system, my script would be thrashed left and right ...

Am just searching for a better way to do that.

Thanks for your reply again. Smilie Smilie
# 4  
Old 11-02-2009
I managed to get this working with a lock file concept in place.

To create a file - presence of which will indicate that the process is created and after the process is done, the next command in sequence is to delete the file. This indirectly means, if there is no file then processing is done.

With this approach its perfectly working - no issues for now Smilie

But to say frankly, I hate this approach like anything
it is not robust
not fool proof
can be hacked
no control of child created via the parent

Am still looking for some help from experts here to get the actual pid that am interested.
# 5  
Old 11-02-2009
Unless you expect the first script to finish within a second or two, frans's solution is fine. Also, if you added a lock file that gets deleted when the process ends, can't you use the same script that creates the lock file to create it with the pid in the lock file's name? Alternative concepts could be to have envVarSetter write the pid of the child and any other useful info to its parent process's read pipe.

Alternatively, you could look through children of init for processes of that name that began a within a second or two of the script's initial running.

etc, etc. There are a lot of possibilities.
# 6  
Old 11-05-2009
Quote:
Originally Posted by thmnetwork
Unless you expect the first script to finish within a second or two, frans's solution is fine.
That needed not be the case, and the solution that am trying for is some generic solution

Quote:
Also, if you added a lock file that gets deleted when the process ends, can't you use the same script that creates the lock file to create it with the pid in the lock file's name?
A lock file with pid in it isn't really making any difference here, just a running number to keep track of the active child count should do and that is what i had done for now

Quote:
Alternative concepts could be to have envVarSetter write the pid of the child and any other useful info to its parent process's read pipe.

Alternatively, you could look through children of init for processes of that name that began a within a second or two of the script's initial running.

etc, etc. There are a lot of possibilities.
Again an important point is being missed out here,
say at t1 the main script spawns a process p1 and is adopted by init at the same time t1, another script with the same name can be spawned and become the child of the init

In a busy node, all these considerations should be dealt well, else there would be confusion as to who is the parent of whom

-- many thanks for the suggestions

---------- Post updated 11-05-09 at 11:27 AM ---------- Previous update was 11-04-09 at 11:34 PM ----------

Now I have an improved way to prevent the hack of spoofing child creation instance by adding new files at least to an extent.

Instead of writing the active child number spawned, write a random number to the file and maintain that in an internal hash. When checking for the existence of file as a synonym that is indicating whether a process is running or not, search for the entry in the file and compare it against the internal hash that is built with this values. With this, spoof of child creating by just touching files can be avoided.

I don't want to write the pid because effectively we are writing the PPID and not PID which should be avoided.
# 7  
Old 11-05-2009
Wrench

Quote:
Originally Posted by matrixmadhan

A lock file with pid in it isn't really making any difference here, just a running number to keep track of the active child count should do and that is what i had done for now
Maybe I was too vague in my initial posting. Let's say you create the file: /tmp/dataCruncher.exe.lock.1928 in the script that creates the lock file. You "parent" script can then look into /tmp for files matching the pattern "dataCruncher\.exe\.lock.([0-9]+)" (or ""dataCruncher.exe.lock followed by a period and one or more numbers.")(or whatever type of regex you plan on using)

in the above PCRE regex, the language/utility/whatever will create a backreference for the number portion of the pattern under "1."

proof of concept in Perl:

Code:
#!/usr/bin/perl -w

our @files = </tmp/dataCruncher.exe.lock*>;
our @pids = ();

foreach $file (@files) {
           $file =~ /dataCruncher\.exe\.lock\.([0-9]+)/;
           push ( @pids, $1 );
}

@pids ends up being an array of PIDs retrieved out of lockfile filenames in such a way.

Hope that makes what I'm saying seem a little clearer.

Quote:
Originally Posted by matrixmadhan

Again an important point is being missed out here,
say at t1 the main script spawns a process p1 and is adopted by init at the same time t1, another script with the same name can be spawned and become the child of the init

In a busy node, all these considerations should be dealt well, else there would be confusion as to who is the parent of whom
Random numbers aren't random. Port numbers aren't guarantees. There is ambiguity in real world applications. Since, as you admit, identifying information is destroyed before you have the chance to save it, you're forced to depend on heuristics, which are fuzzy by their nature, but a best-guess that's known as being a best-guess is better than no idea at all.

Criteria you can examine the process tree for: username, time, command, parent(1), etc. All of which make it less likely that you've caught more than one process in the fray. You also haven't mentioned how you're creating the lockfile. It would seem a process that can know when the children die off can also do the above file naming.

Quote:
Originally Posted by matrixmadhan
I don't want to write the pid because effectively we are writing the PPID and not PID which should be avoided.
Well, I think I can maybe help a little more if I understand more about how you're creating the lockfile.

Last edited by thmnetwork; 11-05-2009 at 08:22 PM.. Reason: fixing syntax error
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding a file process ?

Hi, I am trying to find a file that have a different name than it should be processing, the file name is ( Fifa15 ) is there a command to use? I got that file by ps -ef | grep fifa15 but how do I know what is running ? thanks a lot, I am learning unix so sorry if that is a... (2 Replies)
Discussion started by: latinooo
2 Replies

2. Shell Programming and Scripting

Finding process which ended another process

Hello, The scenario is as follows, I have a background process running initially for which i know the PID on machine1. I use ssh from machine 2 to execute a script in machine 1. For some reason the back ground process is terminated. I would like to know which process caused the... (6 Replies)
Discussion started by: prasbala
6 Replies

3. UNIX for Dummies Questions & Answers

Finding a rogue process

Afternoon all, hopefully someone can give me a hand with this (the following may be explained very poorly :rolleyes: ) I know there's a process running on one of our Solaris 10 boxes that runs approximately every 5 minutes. Unfortunately I've no idea, who owns it, what it is called, or how it is... (2 Replies)
Discussion started by: dlam
2 Replies

4. Shell Programming and Scripting

Finding the process id of the process using the ports

Hi Any idea how to get the process id of the process using the ports lsof -i :portnumber does not work in my machine. I am on sun Solaris SPARC. Any suggestion is highly appreciated (1 Reply)
Discussion started by: kinny
1 Replies

5. Linux

Need help in finding process

Hello, Iam running a apache webserver in CentOS recenlty a hacker has attacked my server using RFI attack and did something in my server.. After that everyday at 8Pm my httpd is using about 5000 pid's actually in normal it takes only about 30 - 40 pid's. and also exim uses 2000 pid's totally my... (2 Replies)
Discussion started by: dheeraj4uuu
2 Replies

6. Shell Programming and Scripting

finding Background Process Id

Hi Gurus, How can i find background process is completed or not. I have mentioned my scenario below. Actually Pr1 Process is running in back ground, i just want to know whether this process completed or not. I can come to know the process id by typing pid=$! but i want to trigger... (4 Replies)
Discussion started by: krk_555
4 Replies

7. UNIX for Dummies Questions & Answers

Finding out process id in a scipt

Hi, If in a shell script i write a command ls > bla & ls The output is redirected to bla and the next ls starts as first one is going on in background. I want to find the PID of the first command. Thanks in advance (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies

8. UNIX for Dummies Questions & Answers

finding process id

is there a way to find the process id of a process because i have same process invoked several times. when i need to kill them, i get confused with the id. Thanks, sskb :( (8 Replies)
Discussion started by: sskb
8 Replies

9. UNIX for Advanced & Expert Users

Finding Out When A Process Has Finished?

Problem I have an application which basically runs lots of UNIX programs remotely, using the Telnet protocol. For each program it remotely executes, it stores the process ID (PID) for that process. At regular intervals, I would like my application to take the PID for every process still... (5 Replies)
Discussion started by: 1cuervo
5 Replies
Login or Register to Ask a Question