Find the answer to your Linux question:
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.
  1. #1
    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?

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,755
    Assuming the script name shows up in the process table...

    Code:
    ps -ef | grep "script_name"

  3. #3
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,414
    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 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,755
    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.

  5. #5
    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.

  6. #6
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,414
    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

  7. #7
    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.

  8. #8
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,755
    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.

  9. #9
    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?

  10. #10
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    682
    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...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •