Capturing the return code from background process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing the return code from background process
# 1  
Old 11-21-2013
Capturing the return code from background process

Hi All,

I was out not working on unix from quite sometime and came back recently. I would really appreciate a help on one of the issue I am facing....

I am trying to kick off the CodeNameProcess.sh in PARALLEL for all the available codes. The script runs fine in parallel.
Let say there are 50 codes. I am kicking off the CodeNameProcess.sh 50 times in parallel as a background process.
This is working fine.

ISSUE:
I am trying to see out of 50 codes, if for any one of the codes, the script CodeNameProcess.sh errors out,
I want to capture return code ($?) for that. All I am trying to do is to capture the return code of all the 50 codes.
So that at the end of all the 50 codes execution, I can do something with the process if any ONE out of 50 codes has failed.

Below is the code snippet below that is kicking off all the codes successfully in parrallel(which is working fine) but
I am not able to get the proper RETURN CODE even if the scripts for one of the code has failed.
Is there a way to capture the return code from CodeNameProcess.sh running in background.

Code:
#!/bin/ksh
ERROR=0

for codes in `cat ${DIR}/filename.txt`
do

	. /scripts/CodeNameProcess.sh $codes &
	rc=$? 
	# I am not able to get the proper return code back from $rc above. 
	#It always show success even if there is an error in CodeNameProcess.sh for one of the codes

	echo "The script launched for ${codes}"


		if [ ${rc} -ne 0 ]
		then
			ERROR=1
			echo "the script for ${codes} FAILED....Please check...."
		fi

done


if [ ${ERROR} -ne 0 ]
then
	 echo "Codename script failed for some of the codes....Please check..."

else
	echo "The Process Complete Successfully for all 50 codes."
	
fi


I would really appreciate your help.

Please Note: The script needs to kick off the CodeNameProcess.sh in parallel for all codes hence kicking it
off as a bacground process: CodeNameProcess.sh &
rkumar28
# 2  
Old 11-21-2013
The $? for getting the return code only works against the last foreground executed task.

With that said, consider the following:

Code:
(/path/to/my/background/script.sh; echo $?) &

Now you may have to do something tricky like capturing the i/o and possibly identify the "job" also so that you can process these afterward and find which one failed, etc....

Hope this is a good enough hint...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to return from background process and check if it is running or not?

Hi Team, i am executing 3 scripts in background from 1 script and i want to send a message once the script gets completed.these scripts usually takes 1 hr to complete. My sample script is below, Vi abc.sh sh /opt/data/Split_1.sh & sh /opt/data/Split_2.sh & sh /opt/data/Split_3.sh & ... (3 Replies)
Discussion started by: raju2016
3 Replies

2. Shell Programming and Scripting

Pass return value of a function in background process

Hi, I have created a function f1 defined in script A.sh .I have called this function in background . But I want to use its return value for another function f2 in script A.sh. I tried declaring it as a global variable, yet it always returns the status as 0. Is there any way with which I can get... (7 Replies)
Discussion started by: ashima jain
7 Replies

3. Shell Programming and Scripting

Catch exit code of specific background process

Hi all, i hava a specific backgroud process. I have de PID of this process. At some time, the process finish his job, is there any way to catch the exit code? I use "echo $?" normally for commands. Thanks! (2 Replies)
Discussion started by: Xedrox
2 Replies

4. Shell Programming and Scripting

Background process, return code and pid.

Hey all, Okay, this one is tricky and I'm not sure there is a niec way to do it, or indeed anyway to do it. The main issue revolves around timing out a hung ssh. I am doing this by creating a wrapper script for the ssh with the following requirements. My requirements are: Defineable... (5 Replies)
Discussion started by: RECrerar
5 Replies

5. UNIX for Dummies Questions & Answers

return code capturing for all commands connected by "|" ...

Hi, While I am using "|" to join multiple commands, I am not getting the return code when there is error in one of the commnads. Eg: b=`find /path/a*.out | xargs basename` if ; then echo "Error" fi if there is error while finding the file or getting the basename, the $? is... (6 Replies)
Discussion started by: new_learner
6 Replies

6. Shell Programming and Scripting

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} & 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} & retcode=$? (3 Replies)
Discussion started by: c19h28O2
3 Replies

7. Shell Programming and Scripting

How to include RETURN KEY with Background process "&" in Shell Script

Hello All, I am a newbie in Shell script programming, and maybe you can help me with my query. I need to write a shell script (mntServer.ksh) that will start a background process and also to be able to run another script. The mntServer.ksh script contains: #!/bin/ksh... (1 Reply)
Discussion started by: racbern
1 Replies

8. Programming

return code of a process

two programs A and B writting in c++ I am using A to B and I want to know the return code of B. in B ------------------------ int main() { return 11; } ------------------------ in A ------------------------ int main() { system(A); } ------------------------ Is it the right way... (1 Reply)
Discussion started by: filedeliver
1 Replies

9. UNIX for Advanced & Expert Users

return code of a process

two programs A and B writting in c++ I am using A to B and I want to know the return code of B. in B ------------------------ int main() { return 11; } ------------------------ in A ------------------------ int main() { system(A); } ------------------------ Is it the right way... (1 Reply)
Discussion started by: filedeliver
1 Replies

10. Shell Programming and Scripting

background process return code

Hi I have the following piece of code that is calling another child process archive.ksh in the background while read file; do file_name=`ls $file`; ksh archive.ksh $file_name &; done < $indirect_file The problem is, indirect_file may contain anwhere from 2 to 20 different... (5 Replies)
Discussion started by: Vikas Sood
5 Replies
Login or Register to Ask a Question