If i have a file called myprogram containingsleep 200dateRun this in the background:$ sh myprogram &i want to know when myprogram has completed by using wait command$ cat >notifywait PIDecho 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
wait does not wait
shell;process;exit
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.