Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a script with loop like

while read host
do
./script &
done

I want to know the process id of the current process which is starting the ./script in background and use it in script.

share|improve this question
    
but $$ does not work inside script which is in background –  munish Apr 15 '13 at 14:06
    
Oops! sorry goldi i nade a mistake mistakenly i edited your comment –  munish Apr 15 '13 at 14:07
    
You didn't; I deleted it and made it an answer. –  goldilocks Apr 15 '13 at 14:10

2 Answers 2

up vote 6 down vote accepted

Assuming a POSIX shell (like bash), $$ is the PID of the current shell and $PPID is the PID of the parent. You can pass $$ to the script or check the value of $PPID in the script.

share|improve this answer

The process ID of the current shell is in the special variable $$.

You can pass this to a forked (backgrounded) process:

./script $$ &

In "script", that will be the first argument, so:

parent_pid=$1

There's a summary of bash special variables here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.