Finding the Process ID of a Process While Initiating the Process
Linux - NewbieThis 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.
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.
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]!
$ 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.
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?
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.
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.)
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.