Find the answer to your Linux question:
Results 1 to 2 of 2
Hi to all, my problem refers to $$ behaviour. If I have this script, in which I try to read bgFunc's PID from itself: Code: #!/bin/sh bgFunc() { echo "bgFunc ...
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Posts
    1

    Background PID introspection

    Hi to all,

    my problem refers to $$ behaviour. If I have this script, in which I try to read bgFunc's PID from itself:

    Code:
    #!/bin/sh
    
    bgFunc()
    {
       echo "bgFunc PID: $$"
    }
    
    
    #main
    
    bgFunc &
    echo "spawned bgFunc PID: $!"
    echo "main PID: $$"
    PID returned from $$ in bgFunc is equal to main's PID, and NOT to "spawned bgFunc PID".

    How can I read bgFunc's PID from bgFunc itself?

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    bigfunc() is a shell function, not a process, so $! will naturally return the shell's pid. IE, $! will equal $$.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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