Domanda

I am running shells cascading 3 wrappers.Conceptually like this

outside.ksh <parameters>
    middle.ksh <parameter>
     inner1.ksh <para> > Log 2>&1 &
     inner2.ksh <para> > log 2>&1 &
     inner3.ksh <para> > log 2>&1 &
     sleep nnn 
      innern.ksh <para> > log 2>&1 &

The sleeping controls # threads set off in parallel. My understanding is because each inner shell is fired off independently in the background - if I use wait instead of sleep , wait will just respond to the last thread 1 block ( inner3) . The earlier 2 might still be running but can wait actually 'wait' for all these background processes to finish ?

The 2nd question I have is regards return code. Each of these inner shells will return their exit codes ( $?) when asked but I'd like 1 final code to be returned by the middle shell if ALL these are successful and a non zero if ANY of them failed. My understanding is that once again- the middle.ksh's return code aligns with the last inner shell fired. How do I get it to account for all inner.ksh return status

È stato utile?

Soluzione

if I use wait instead of sleep , wait will just respond to the last thread 1 block ( inner3) . The earlier 2 might still be running but can wait actually 'wait' for all these background processes to finish ?

wait with no arguments will wait for all child processes to exit. Its return status will be 0.

The 2nd question I have is regards return code. Each of these inner shells will return their exit codes ( $?) when asked but I'd like 1 final code to be returned by the middle shell if ALL these are successful and a non zero if ANY of them failed. My understanding is that once again- the middle.ksh's return code aligns with the last inner shell fired. How do I get it to account for all inner.ksh return status

You can use three separate waits and then combine their return codes into one. If you use job IDs like %1 then you don't need to remember the individual PIDs either.

inner1.ksh <para> > Log 2>&1 &
inner2.ksh <para> > Log 2>&1 &
inner3.ksh <para> > Log 2>&1 &

wait %1; status1=$?
wait %2; status2=$?
wait %3; status3=$?

# Combine the three statuses into one with binary OR. This will only be 0 if
# all three commands returned 0.
exit $((status1 | status2 | status3))

If you don't like combining them with |, you could use this instead:

exit $((status1 == 0 && status2 == 0 && status3 == 0 ? 0 : 1))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top