passing return code to parent shell based on conditions
I am running a wrapper within a wrapper with the innermost having all my commands Like this
L4.ksh # topmost wrapper L3.ksh L2.ksh Nohup command > log 2>&1& # label that c1 Nohup command > log 2>&1& # c2 wait # w1 Nohup command > log 2>&1& # c3 L2_1.ksh Nohup command > log 2>&1& # label that c4 Nohup command > log 2>&1& # c5 wait # w2 Nohup command > log 2>&1& # c6
what I want to do is capture exit status of C6 and C3. of course I will stick a rc=$? below it .Ok just did that . Next -I want that when ALL commands L2 get over at that time Look at the status of C3 which is the LAST nohup in L2 and exit code of L2 should be that of C3 AFTER EVERYTHING in L2 has completed . Note these C1 C2 etc are all run in parallel. When L2 finishes I will execute L2_1 the same way only after L2 finishes completely that is all processes in it finish off ( so C3 could have finished and C1 C2 are still running - but all C's should finish off is the reqd. criteria ) Finally if the status codes of L2 and L2_1 are both 0 then L3 should return 0 else l3 should return non zero status code.
if I put capture the $? of the last command in L2 series ( C3 and C6 ) and then put wait and then exit $rc
C3 rc=$? wait exit $rc
for L2 and L2_1 both and then do a conditional switch for status code of L3- would that serve the purpose ? or how best can it be done ty sam
Thanks Dogbane for your reply to capture individual PID's. The problem is that I dont know how many shells get fired. L2.ksh is a run-time generated shell that contains commands so the question is how do I collect all PID's.I'd have to start some counter ? to keep track. Isn't there a better way of finding out the status when all background tasks have finished their work in a shell and then flag exit status of the last background task fired ?
I suggest you capture pids and wait on them as shown below:
L2.ksh nohup command > log 2>&1& # label that c1 c1_pid=$! nohup command > log 2>&1& # c2 c2_pid=$! nohup command > log 2>&1& # c3 c3_pid=$! # wait for c1 and c2 to finish. wait $c1_pid $c2_pid # wait for c3 to finish wait $c3_pid exit $?