错误处理并行执行多个子shell脚本

时间:2016-05-27 21:59:21

标签: shell unix error-handling parallel-processing ksh

我有几个需要并行运行的子shell脚本。要并行运行这些脚本,我使用以下命令

if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = array();
}
$bag = array(
    "sessionId" => session_id(),
    "productId" => $productId, 
    "size"      => $productSize,
    "quantity"  => $productQuantity
);
$_SESSION['cart'][] = $bag;

如何处理上述每个子shell脚本的错误。如果所有3个子shell脚本都成功,我希望将状态返回为成功,反之亦然。每个子shell应在100秒后执行。但是使用上面的语法,我只能获得上次执行过程的状态。

请帮我解决这个问题。

注意:我没有GNU并行

提前致谢

2 个答案:

答案 0 :(得分:0)

我将“每个子shell应该在100秒后执行”解释为“100秒后被杀死”:

results=/tmp/results_
rm ${results}* 2>/dev/null

date
(sleep 4; echo $? > ${results}1)&
subproc_1=$!
(sleep 9;  echo $? > ${results}2)&
subproc_2=$!
(sleep 5; false; echo $? > ${results}3)&
subproc_3=$!

(sleep 100; kill ${subproc_1} ${subproc_2} ${subproc_3}) &
wait ${subproc_1} ${subproc_2} ${subproc_3}

date
for i in {1..3}; do
   # only display result when resultfile exists
   test -f ${results}$i && printf "Process %d: Result %d\n" $i $(cat ${results}$i)
done

答案 1 :(得分:0)

您可以使用以下简单模式等待每个脚本并收集其退出状态:

abc.sh Hi   & PID1=$!
abc.sh Hello& PID2=$!
abc.sh how  & PID3=$!

if wait $PID1 && wait $PID2 && wait $PID3
then    flag=SUCCESS
else    flag=FAIL
fi