Results 1 to 10 of 10
A c program is invoking a shell script.I need to find out the process id of the program responsible for invoking the shell script.Is this possible?...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 05-01-2008 #1Just Joined!
- Join Date
- Apr 2008
- Posts
- 7
to find process id of program invoking a shell script.
A c program is invoking a shell script.I need to find out the process id of the program responsible for invoking the shell script.Is this possible?
- 05-01-2008 #2Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,722
Assuming the script name shows up in the process table...
Code:ps -ef | grep "script_name"
- 05-01-2008 #3
FYI: A shorter (but otherwise identical) way to do that is
The original post gives the impression that the name of the program invoking the shell script is not known, and that this is what he wants to find. Try looking at ps in tree viewCode:pgrep script_name
This will output a tree view of all process to a text file which you can then search for the name of the shell script to see what is calling it.Code:ps -ejH > tempfile.txt
Registered Linux user #388328 || Registered LFS user #15880
AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
Need instant help? Try us on IRC -- #linuxforums on freenode
- 05-01-2008 #4Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,722
If one process calls a script, the script will *generally* show up in the process table, which is what was asked:
Code:ps -ef | grep cron
Here, PID 1 (init) called the "cron" script, which is running as PID 2712.Code:root 2712 1 0 Apr14 ? 00:00:00 /usr/sbin/cron
Thus, if I wanted to know "what process called cron," I now know that it's PID 1. If I didn't know what PID 1 was, I'd follow it up with:
Either method works.Code:ps -ef | grep " 1 "
- 05-02-2008 #5Just Joined!
- Join Date
- Apr 2008
- Posts
- 7
- 05-02-2008 #6
But you know the name of the shell script that is being invoked, so you use the ps command HROAdmin26 gave you, and look for the PID of the process that called it. Just as HROAdmin26 explained.
If you're not sure when the process is going to be called, you could put that command at the top of the shell script in such a way that the information is written out to a file.Registered Linux user #388328 || Registered LFS user #15880
AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
Need instant help? Try us on IRC -- #linuxforums on freenode
- 05-02-2008 #7Just Joined!
- Join Date
- Apr 2008
- Posts
- 7
[QUOTE=khuranarahul;583317]A c program is invoking a shell script.I need to find out the process id of the program responsible for invoking the shell script.Is this possible?i need a solution such that as soon as the program invokes the shell script i come to know the process id of the program.i'm not aware which program is invoking the shell script.with the help of the process id i can trace the program.
- 05-02-2008 #8Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,722
^^^ What Smolloy said.If you're not sure when the process is going to be called, you could put that command at the top of the shell script in such a way that the information is written out to a file.
You have said you don't know the process calling the script. Do you know the name of the script being called? If the script is running, you can search ps for the script name. If you don't know *when* the script is being called, add a command to the script that writes the calling process name and ID to a text file and you can read it later.
Add to script something like:
Process <$CALLING_PROCESS> called me at $DATE >> $FILE
You may have to get the $PID of the calling process, then search ps for that PID to get the exact process name.
- 05-03-2008 #9Just Joined!
- Join Date
- Apr 2008
- Posts
- 7
to find the output of echo $!
what is the output of the command echo $!.
also do we have any arguments with the echo command to print the process id or the parent process id?
- 05-04-2008 #10Linux Enthusiast
- Join Date
- Apr 2004
- Location
- UK
- Posts
- 678
Might do what you need. -o selects the fields to output. -p selects the process you are interested in, and $$ is the current pid of the shell script.Code:chris@angua:~$ ps -o ppid -p $$ PPID 23376
Chris...To be good, you must first be bad. "Newbie" is a rank, not a slight.


Reply With Quote

