Operating System - HP-UX
1834724 Members
2555 Online
110069 Solutions
New Discussion

Re: How to know when a background task is complete?

 
SOLVED
Go to solution
John Chaff
Advisor

How to know when a background task is complete?

Hello,

Inside a script, I start a another script in the background so I can continue processing. Is there a way besides ps to find out when this command has finished?

Thanks,
John
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to know when a background task is complete?

This is pretty much Duck Soup.

mybackground.sh &
CHILDPID=${!}

Now all you have to do is periodically:
kill -0 ${CHILDPID}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Kid is still going"
else
echo "Kid is dead"
fi

Man 1 kill for details.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to know when a background task is complete?

Plan B.

Have the child send a signal via kill to the parent process (using ${PPID}) just before the child terminates. The parent script, in turn, has a trap for the chosen signal. This is probably a better method because you no longer have to loop inside the parent checking on the status of the child.


If it ain't broke, I can fix that.
Sundar_7
Honored Contributor

Re: How to know when a background task is complete?

Hi John,

Alternatively, at some point of time, if you have to just wait on the background process you started to end, then you can use the wait command.

man wait for details

Sundar.
Learn What to do ,How to do and more importantly When to do ?
John Chaff
Advisor

Re: How to know when a background task is complete?

Thanks. The kill -0 was just what I needed.

John