processUsageFile()
{
#sdate=`pin_virtual_time | awk -F" " '{print $3}'`;
#Get all new files to be loaded to brm staging data.
count=`ls ${PRE_STAGING}/TWN* 2>/dev/null|grep -v reprocess|wc -l`
if [ $count -ne 0 ];then
# Fork subshell
(./efx_omc_brm_rpt_process.sh -t TWN & )&
exitOnError
fi
#Process Rapid Report files
count=`ls $PRE_STAGING/RR* 2>/dev/null|grep -v reprocess|wc -l`
if [ $count -ne 0 ];then
(./efx_omc_brm_rpt_process.sh -t RR &)&
exitOnError
fi
...
...
}
#Reprocessing. Process the reprocessed files.
#This method updates the records in the BRM staging table.
reprocessingUsageFile()
{
#Process TWN fulfillment reprocess files
count=`ls $PRE_STAGING/TWN*reprocess* 2>/dev/null|wc -l`
if [ $count -ne 0 ];then
# Fork subshell
(./efx_omc_brm_rpt_reprocess.sh -t TWN & ) &
fi
#Process Rapid Report files
count=`ls $PRE_STAGING/RR*reprocess* 2>/dev/null|wc -l`
if [ $count -ne 0 ];then
(./efx_omc_brm_rpt_reprocess.sh -t RR &) &
fi
...
...
}
#Pre processing
PreProcessing
# Start processing usage files.
processUsageFile
processErrFile 上面代码的思想是进行并行处理。所有方法都调用多个子subshells并从tty中分离。我想知道是否有办法等待前两个方法先完成执行,然后再运行最后一个方法。
等待PID在某种程度上是不准确的。还在努力..。
waitPids() {
echo "Testing $pids -- ${#pids[@]}"
while [ ${#pids[@]} -ne 0 ]; do
local range=$(eval echo {0..$((${#pids[@]}-1))})
local i
for i in $range; do
if ! kill -0 ${pids[$i]} 2> /dev/null; then
echo "Done -- ${pids[$i]}"
unset pids[$i]
fi
done
pids=("${pids[@]}")
sleep 1
done
}发布于 2016-03-19 02:57:38
使用等待构建
$ help wait
wait: wait [-n] [id ...]
Wait for job completion and return exit status.
Waits for each process identified by an ID, which may be a process ID or a
job specification, and reports its termination status. If ID is not
given, waits for all currently active child processes, and the return
status is zero. If ID is a a job specification, waits for all processes
in that job's pipeline.
If the -n option is supplied, waits for the next job to terminate and
returns its exit status.
Exit Status:
Returns the status of the last ID; fails if ID is invalid or an invalid
option is given.极简主义示例
$ wait -n; (sleep 3; false); echo $?
1您的代码示例
后台任务立即返回。您的诀窍是将函数包装在一个子subshell中,这样您就可以等待该子subshell (而不是后台作业)完成。例如:
$ wait -n; (processUsageFile); echo $?如果想要更复杂,就必须在变量中捕获后台任务的PID,这样就可以使用wait $pidof_process_1 $pidof_process_2这样的结构等待特定的进程。
将函数包装在子subshell中会更容易。但是,您的特定需求可能会有所不同。
发布于 2016-03-19 03:14:10
看起来主要的问题是,你使用的是分离子外壳。
也许最简单的解决方案是使用不同的机制来分离子subshells,这样您就可以使用wait。
例如,通过nohup
nohup ./process1 &
nohup ./process2 &
wait发布于 2016-03-19 02:49:36
可能是进程和重新进程之间的“等待”命令。
来自:http://www.tldp.org/LDP/abs/html/subshells.html
例21-3。在子subshells中运行并行进程
(cat list1 list2 list3 | sort | uniq > list123) &
(cat list4 list5 list6 | sort | uniq > list456) &
# Merges and sorts both sets of lists simultaneously.
# Running in background ensures parallel execution.
#
# Same effect as
# cat list1 list2 list3 | sort | uniq > list123 &
# cat list4 list5 list6 | sort | uniq > list456 &
wait # Don't execute the next command until subshells finish.
diff list123 list456https://stackoverflow.com/questions/36091994
复制相似问题