LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-27-2009, 09:00 AM   #1
senthilmuthiah
Member
 
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56

Rep: Reputation: 16
Question Finding the Process ID of a Process While Initiating the Process


Dear All:

I have a program in java which I execute using a shell script. I would like to find the process id of this java process immediately after execution so that I could kill it downstream somewhere. I was browsing through pgrep/grep etc., However, the problem here is, since this is a java process the name of the process itself is given as "Java" (which I came to know after using the 'top' command. So, I can't use pgrep/grep/ps since there would be other java processes running in the system which I wouldn't dare to even touch!

So, I was wondering whether there is a way by which I could extract the Process ID while I initiate the process itself [as I know there is no way by which I can assign a PID for my forked process]!

Any help would be appreciated!

Thanks in advance!
 
Old 03-27-2009, 10:13 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

The PID of the last process spawned in your shell is put in '$!', so:
Code:
#!/bin/bash

java mainclass &
JAVAPID=$!
...
kill $JAVAPID
Dave
 
Old 03-27-2009, 10:15 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
From the bash man page:

Quote:
$ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the sub-shell.

! Expands to the process ID of the most recently executed background (asynchronous) command.
So if you type:
echo $$ - it will show you PID of current shell.

If you then ran a background process like:
while true; do ls -l; sleep 300; done &
and then typed:
echo $! - it will show you the PID of the while loop above.
 
Old 03-27-2009, 10:23 AM   #4
senthilmuthiah
Member
 
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56

Original Poster
Rep: Reputation: 16
Thumbs up

Thanks a lot jlightner and ilikejam. You guys are wonderful!

Both the solutions are working.

Thank you very much!
 
Old 04-01-2009, 01:57 PM   #5
senthilmuthiah
Member
 
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56

Original Poster
Rep: Reputation: 16
Question

Hi jlightner and ilikejam! Seems, there is a small problem here:

I have a script like the following:

ssh -f abc@xyz.edu 'cd /home/abc/; nohup java RunSim '$toSimulate' & JAVAParent='$!' ; echo '$JAVAParent' > ParentPID.txt; pgrep -u '$useid' -P '$JAVAParent' > SimulationPID.txt'

Basically, the script runs a java program (RunSim, which in itself initiates another java program) in a client xyz. I wanted the process id of this parent process so that I could get the PID of the child process through pgrep/ps.

When I debug the script using bash -x, I found that $! wasn't given any value => JAVAParent was empty! Any idea of how I could get through this?

Looking forward to your replies very much!
 
Old 04-01-2009, 02:05 PM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
I'm assuming that $toSimulate and $useid are set on the machine you're sitting at, not on xyz.edu, so you'd want them to be evaluated before the command is sent over. I'm also assuming there's no spaces in the values of those variables.

ssh -f abc@xyz.edu 'cd /home/abc/; nohup java RunSim '$toSimulate' & JAVAParent=$! ; echo $JAVAParent > ParentPID.txt; pgrep -u '$useid' -P $JAVAParent > SimulationPID.txt'

Dave
 
Old 04-02-2009, 10:19 AM   #7
senthilmuthiah
Member
 
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56

Original Poster
Rep: Reputation: 16
Thank you very much ilikejam! That problem is gone, however, there is a new one propping up!

The ParentPID.txt is written well while the SimulationPID.txt has nothing in it. I executed
pgrep -u <userid> -P <ParentIDwritteninParentPID> SimulationPID.txt
separately in the client and it does seem to give me the correct PID of the child.

Is it because the SimulationPID.txt is created even before the child process is started? If yes, is there any way by which I could extract this information?

Also, just to make it clear, can you describe what you mean by 'evaluate' in this discussion (It would also help some newb who is viewing this thread.)

Thanks in advance!
 
Old 04-02-2009, 10:37 AM   #8
senthilmuthiah
Member
 
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56

Original Poster
Rep: Reputation: 16
Talking

Quote:
Originally Posted by senthilmuthiah View Post
Thank you very much ilikejam! That problem is gone, however, there is a new one propping up!

The ParentPID.txt is written well while the SimulationPID.txt has nothing in it. I executed
pgrep -u <userid> -P <ParentIDwritteninParentPID> SimulationPID.txt
separately in the client and it does seem to give me the correct PID of the child.

Is it because the SimulationPID.txt is created even before the child process is started? If yes, is there any way by which I could extract this information?

Also, just to make it clear, can you describe what you mean by 'evaluate' in this discussion (It would also help some newb who is viewing this thread.)

Thanks in advance!
So, the problem was indeed what I had mentioned, I gueess: I am writing SimulationPID.txt even before the child process is initiated (so as expected, I don't get anything).

I included a "sleep 5m" in between the two commands and now I see it!

Thank you all for all the help. I love LQ!
 
  


Reply

Tags
extract, id, pid, process



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
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
Finding process id of last crashed process santoshbr4 Linux - Newbie 1 02-16-2009 08:06 AM
scheduler invocation, process selected, process entry and exit linuxdoniv Programming 1 03-14-2008 03:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:28 PM.

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