等待子shell进程完成。

6
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 

以上代码的想法是进行并行处理。所有方法都会调用多个子shell并从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
    }

1
这里使用wait有帮助吗? - Etan Reisner
@EtanReisner 是的,等待会有帮助,但我没有得到准确的结果。首先,子shell是分离的,因此我必须跟踪每个PID并将它们添加到数组中,然后运行循环以确保“等待PID”所有子进程都已完成。但不知何故,这对我来说不起作用。 - user2570205
啊,你正在进行双重分支,所以它们不是子进程。 - Etan Reisner
4个回答

8
似乎主要问题是您正在使用分离的子shell。可能最简单的解决方案是使用不同的机制来分离子shell,这样您就可以使用“wait”命令。例如,通过“nohup”命令。
 nohup ./process1 &
 nohup ./process2 &
 wait

我同意这个。 - user2570205
@user2570205 如果您同意,应该点赞/接受答案,而不是留下评论:回报社区... - umläute
我没有足够的特权/声望来点赞。我只同意这一个,但这不是解决方案。 - user2570205

7

使用等待内置功能

$ 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

以您的代码为例

后台任务会立即返回。对于您来说,技巧在于将函数包装在子shell中,这样您就等待 子shell(而不是后台作业)完成。例如:

$ wait -n; (processUsageFile); echo $?

如果你想变得更加复杂,你需要将后台任务的PID捕获到变量中,这样你就可以使用类似wait $pidof_process_1 $pidof_process_2的结构等待特定的进程。
将函数包装在子shell中只是更容易。但是,你的具体需求可能会有所不同。

在分叉子Shell之后,控制将立即从函数中退出。 - user2570205

1
可能是在进程和重新处理之间使用'wait'命令。

from: http://www.tldp.org/LDP/abs/html/subshells.html

示例 21-3. 在子shell中运行并行进程

(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 list456

我知道这个例子,但是它对我的情况没有帮助。 - user2570205
1
在这里解释一下为什么等待不起作用可能会带来更多的选择。 - tabbek
因为子shell是分离的。在你的例子中,它只是确保它们在后台运行。 - user2570205

0
我发现最好的并行等待方法是导出一个函数到子shell中使用,然后使用带有-P选项的xargs进行最大数量的并行线程,并使用 -n 或 -L 将特定数量的参数传递给工作函数。
来自: https://man7.org/linux/man-pages/man1/xargs.1.html
       -P max-procs, --max-procs=max-procs
              Run up to max-procs processes at a time; the default is 1.
              If max-procs is 0, xargs will run as many processes as
              possible at a time.  Use the -n option or the -L option
              with -P;

示例代码:

# define some work function and export it
function unit_action() {
  echo action $*
  sleep 5
  echo action $* done
}
export -f unit_action

# list all arguments to feed into function
# with 2 parameters at a time in a maximum of 3 parallel threads
echo {1..9} | xargs -t -n 2 -P 3 bash -c 'unit_action $@' --
echo all done

xargs会隐式等待直到所有输入都被消耗,因此不需要显式的等待命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接