Return code of background process

Hi,

I have a process that I run in the background that looks like this


${BASEDIR}/ksh/sqler.ksh   ${compnames003[$i]} &

and I would like to get the return code of the sqler.ksh script.

so my code is like this


${BASEDIR}/ksh/sqler.ksh   ${compnames003[$i]} &

retcode=$?

but this always returns 0 becuase the last statment it run was the & so is there a way to get the return code of the sqler script rather than the return code of the background process.

If I remove the background processing it works..

so


${BASEDIR}/ksh/sqler.ksh   ${compnames003[$i]}

retcode=$?

works but its not running in the background

Thanks

The return code can only be reaped when the process finishes. Have a look at the wait command. (You will want to wait on that specific process; you get its process ID in $! when you have just started it.)

Hey era,

I was using wait but without the pid, I just literally had "wait" directly underneath.

but now I've changed it to waid for the pid and its now returning the failure code! :slight_smile:

Out of interest is the reason it only returns 0 because it doesn't know what process your referring to if you just use wait witout the pid?

Thanks for the help.

bash$ help wait
wait: wait [n]
    Wait for the specified process and report its termination status.  If
    N is not given, all currently active child processes are waited for,
    and the return code is zero.  N may be a process ID or a job
    specification; if a job spec is given, all processes in the job's
    pipeline are waited for.