Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > The Linux Kernel
Reload this Page to find process id of program invoking a shell script.
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

The Linux Kernel Compiling, theory, programming or other discussion about the linux kernel

Reply
 
Thread Tools Display Modes
Old 05-01-2008   #1 (permalink)
khuranarahul
Just 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?
khuranarahul is offline   Reply With Quote
Old 05-01-2008   #2 (permalink)
HROAdmin26
Linux User
 
Join Date: Nov 2007
Posts: 388
Assuming the script name shows up in the process table...

Code:
ps -ef | grep "script_name"
HROAdmin26 is offline   Reply With Quote
Old 05-01-2008   #3 (permalink)
smolloy
Linux Guru
 
smolloy's Avatar
 
Join Date: Apr 2005
Location: San Francisco, CA, but from Belfast, N.Ireland.
Posts: 1,741
FYI: A shorter (but otherwise identical) way to do that is
Code:
pgrep script_name
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 view
Code:
ps -ejH > tempfile.txt
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.
__________________
Registered Linux user #388328 || Registered LFS user #15880
AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 7300 GT 512MB :: ASUS M2N32 Deluxe :: 2X250GB SATAII
Steve and Lisa online
Steve's Blog
smolloy is offline   Reply With Quote
Old 05-01-2008   #4 (permalink)
HROAdmin26
Linux User
 
Join Date: Nov 2007
Posts: 388
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
Code:
root      2712     1  0 Apr14 ?        00:00:00 /usr/sbin/cron
Here, PID 1 (init) called the "cron" script, which is running as PID 2712.

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:

Code:
ps -ef | grep " 1 "
Either method works.
HROAdmin26 is offline   Reply With Quote
Old 05-02-2008   #5 (permalink)
khuranarahul
Just Joined!
 
Join Date: Apr 2008
Posts: 7
Quote:
Originally Posted by HROAdmin26 View Post
Assuming the script name shows up in the process table...

Code:
ps -ef | grep "script_name"
but i'm not aware which program is invoking the shell script.i need a solution such that as soon as any program invokes the shell script ,i come to know it's process id.
khuranarahul is offline   Reply With Quote
Old 05-02-2008   #6 (permalink)
smolloy
Linux Guru
 
smolloy's Avatar
 
Join Date: Apr 2005
Location: San Francisco, CA, but from Belfast, N.Ireland.
Posts: 1,741
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 7300 GT 512MB :: ASUS M2N32 Deluxe :: 2X250GB SATAII
Steve and Lisa online
Steve's Blog
smolloy is offline   Reply With Quote
Old 05-02-2008   #7 (permalink)
khuranarahul
Just 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.
khuranarahul is offline   Reply With Quote
Old 05-02-2008   #8 (permalink)
HROAdmin26
Linux User
 
Join Date: Nov 2007
Posts: 388
Quote:
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.
^^^ What Smolloy said.

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.
HROAdmin26 is offline   Reply With Quote
Old 05-03-2008   #9 (permalink)
khuranarahul
Just 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?
khuranarahul is offline   Reply With Quote
Old 05-04-2008   #10 (permalink)
kakariko81280
Linux Enthusiast
 
Join Date: Apr 2004
Location: UK
Posts: 517
Code:
chris@angua:~$ ps -o ppid -p $$
 PPID
23376
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.

Chris...
__________________
DRM keeps an honest user loyal
kakariko81280 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 07:17 AM.

Powered by vBulletin 3.6.8 ©2000 - 2007, content relevant URLs by vBSEO, Property of Core Root.

Content Relevant URLs by vBSEO 3.0.0