Here is my script:
#! /bin/bash
ret=0
file_out1=$(mktemp)
file_out2=$(mktemp)
(echo first start; sleep 2s; echo first finish;) &> $file_out1 &
ret=$?
P1=$!
(echo second start; sleep 1s; echo second finish;) &> $file_out2 &
ret=$?
P2=$!
wait $P1 $P2 || ret=$?
cat $file_out1
cat $file_out2
exit $ret
Here, the output is:
first start
first finish
second start
second finish
But the output should be:
second start
second finish
first start
first finish
What I mean is, cat $file_out1
should run as soon as P1
is complete and cat $file_out2
should run as soon as P2
is complete.
Moreover, I want to make sure P1 ended successfully, and P2 ended successfully. At the end, how can I know if there was an error in P1
or P2
or both.
In this script, $ret
is not working properly (it is always exit 0
). After it works, It would be nice to know whether the $ret
was from P1
or P2
. Say something like P1
has return code x1, P2
has return code x2. And exit the program with a return code which is non-zero if there is a non-zero exit code.
Update1:
One answer in askubuntu, suggests that we can use --group
argument of parallel command to ensures, that the output of each command waits till the command has finished (avoids mixed output).
The command it showed is parallel --group 'echo -n {};sleep {};echo {}' ::: 1 3 2 4
.
This command works in my machine. However, I am not understanding how to use this command in my context.
I mean how can I get
second start
second finish
first start
first finish
When the input commands are echo first start; sleep 2s; echo first finish;
and echo second start; sleep 1s; echo second finish;
using gnu parallel.
Update 2:
The code I am currently at is:
#! /bin/bash
function first {
echo first start
sleep 2s
echo first finish
}
function second {
echo second start
sleep 1s
echo second finish
}
export -f first && export -f second
parallel -j2 ::: first second
This actually solves my problem.