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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-27-2009, 09:00 AM
|
#1
|
Member
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56
Rep:
|
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!
|
|
|
03-27-2009, 10:13 AM
|
#2
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
Hi.
The PID of the last process spawned in your shell is put in '$!', so:
Code:
#!/bin/bash
java mainclass &
JAVAPID=$!
...
kill $JAVAPID
Dave
|
|
|
03-27-2009, 10:15 AM
|
#3
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
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.
|
|
|
03-27-2009, 10:23 AM
|
#4
|
Member
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56
Original Poster
Rep:
|
Thanks a lot jlightner and ilikejam. You guys are wonderful!
Both the solutions are working.
Thank you very much! 
|
|
|
04-01-2009, 01:57 PM
|
#5
|
Member
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56
Original Poster
Rep:
|
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!
|
|
|
04-01-2009, 02:05 PM
|
#6
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
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
|
|
|
04-02-2009, 10:19 AM
|
#7
|
Member
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56
Original Poster
Rep:
|
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!
|
|
|
04-02-2009, 10:37 AM
|
#8
|
Member
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56
Original Poster
Rep:
|
Quote:
Originally Posted by senthilmuthiah
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!
|
|
|
All times are GMT -5. The time now is 08:28 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|