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.

If i have a file called myprogram containing

sleep 200
date

Run this in the background:

$ sh myprogram &

i want to know when myprogram has completed by using wait command

$ cat >notify
wait PID
echo "Program completed"
$ sh notify &

PID is the process ID given from third command, my problem is that the message "program completed" printed on the terminal immediately after the last command,why wait does not wait? , i am using ubuntu 11.10

share|improve this question
1  
I'm pretty sure wait can only wait for child processes, which means it must be run in the parent shell, not a separate one like you are doing with the sh wait. –  jw013 Aug 16 '12 at 15:59
add comment

1 Answer

up vote 4 down vote accepted

The child-parent relationship gets ruined by all the '&' involved.

The wait(2) system call, which the wait builtin is apparently based on, will only work for a direct child PID of the process that calls wait(2).

When you invoke sh myprogram &, you run sh as a child of the interactive shell.

When you invoke sh notify &, that sh is a child of the interactive shell.

So the second sh has no direct parent relationship with the first sh, and wait will not work.

This won't even work if you invoke sh myprogram &; sh notify; either, because the wait command is invoked by a child of the interactive shell.

What does work for me is this: sh myprogram &; wait The wait command causes the interactive shell (parent of the sh invoked explicitly) to hang around until the sh exits.

share|improve this answer
add comment

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.