Operating System - HP-UX
1827452 Members
3767 Online
109965 Solutions
New Discussion

Getting pid and return code of the called script within script

 
SOLVED
Go to solution
Rui Vilao
Regular Advisor

Getting pid and return code of the called script within script

Greetings,

I am writing a shell script in which I need to call another shell script.
However I need to check the pid and the return code of the called script.

I should look like as:

$WORKING_DIRECTORY/script.sh &
pid=$!
wait
ReturnCode=$?
...


My problem is that I am not able to get in ReturnCode the exit code of script.sh?

Any help/suggestion is highly appreciated.

Thanks in advance for your help,

Kind Regards,

Rui Vilao.
"We should never stop learning"_________ rui.vilao@rocketmail.com
3 REPLIES 3
renarios
Trusted Contributor

Re: Getting pid and return code of the called script within script

Hi Rui,

I created a same construction, only with functions.
In the function called in the background, I fill the variable PROG, which I monitor in the function below.
TIMEOUT=10
MAX_TIMEOUT=3600 #one hour
# Monitor the proces
while (( $(UNIX95= ps -xC $(basename ${PROG}) |grep ${ORACLE_SID} |wc -w) > 0 ))
do
# wait for the timeout period
sleep ${TIMEOUT}

# update counter with one
(( counter +=1 ))

# calculate total waiting time
(( wait_time=${counter}*${TIMEOUT} ))

# Show feedback as a dot every timeout time
printf "."

if (( ${wait_time} > ${MAX_TIMEOUT} ))
then
print "Killing ${ORACLE_SID} backup process after ${wait_time} seconds"
kill ${backup_process}
fi
done

Another possibility is to use the command jobs, i.e. :
while (( $(jobs | wc -l) > 0 ))
do
# wait for the timeout period
sleep ${TIMEOUT}

# update counter with one
(( counter +=1 ))

# calculate total waiting time
(( wait_time=${counter}*${TIMEOUT} ))

done

Hope it helps,

Renarios
Nothing is more successfull as failure
Solution

Re: Getting pid and return code of the called script within script

Rui,

First off, to be careful you should do this:

$WORKING_DIRECTORY/script.sh &
pid=$!
wait $pid
ReturnCode=$?

IIRC, one of the problems with the wait command is that it only returns the RC of the child process if it is executed *before* the script exits. Given the way UNIX functions, this will not necessarily be the case - (if script.sh exits very quickly for instance). \

In these situations I've always had my child processes write their status to a temp file which I can the read from the parent process - a kludge I know, but much more likely to work.

HTH

Duncan

I am an HPE Employee
Accept or Kudo
Rui Vilao
Regular Advisor

Re: Getting pid and return code of the called script within script


Prblem solved thanks to Duncan & Renarios!

Cheers,

Rui.
"We should never stop learning"_________ rui.vilao@rocketmail.com