【问题标题】:Bash follow multi process returnsBash 跟随多进程返回
【发布时间】:2018-08-03 08:58:15
【问题描述】:

我有一个启动多进程的 bash 脚本:

while read p || [[ -n $p ]]; do
    hostname=$(echo $p | cut -d';' -f1)
    # On lance les actions en background/parallele
    ssh "$hostname" "someAction.sh" > "$hostname.status.tmp" &
done < hostnames.prm 

“someAction”脚本在每台服务器上需要一些时间才能启动。 它逐步返回步骤以说明发生了什么。

我想逐步显示每个进程上每个动作的状态,刷新屏幕。

示例:

Hostname n°1 : 
Step 1 of someAction
Step 2 of someAction
...
Hostname n°2 : 
Step 1 of someAction
Step 2 of someAction
Step 3 of someAction
Step 4 of someAction
someAction finished
...
Hostname n°3 : 
Step 1 of someAction
...
Hostname n°4 : 
Step 1 of someAction
Step 2 of someAction
...
Hostname n°5 : 
Step 1 of someAction
Step 2 of someAction
Step 3 of someAction
Step 4 of someAction
someAction finished

所以我将返回的进度步骤按主机名存储在一个文件中:

ssh "$hostname" "someAction..." > "$hostname.status.tmp" &

但是,当至少有一个进程正在进行时,我该如何显示它们?

这似乎很难:/

【问题讨论】:

    标签: bash shell ssh pid tail


    【解决方案1】:

    感谢您的回答。

    我只是使用:

        clear (go on top of screen)
        ...
        processing=1
    actionPids=()
    
    i=0
    while read p || [[ -n $p ]]; do
        hostname=$(echo $p | cut -d';' -f1)
        type=$(echo $p | cut -d';' -f2)
        if [[ "$type" = "$ENVIR" || "$ENVIR" = "ALL" || "$hostname" = "$ENVIR" ]]
        then
            # On lance les actions en background/parallele
            doInstance "$hostname" "$ACTION" > "$hostname.status.tmp" &
            actionPids[$i]=$!
        fi
        i=`expr $i + 1`
    done < hostnames.prm
    
    lastNbLines=0
    
    tput sc
    
    while [ $processing -eq 1 ]; do
            lastNbLinesTmp=$(cat *.status.tmp | wc -l)
            if [ $lastNbLinesTmp != $lastNbLines ]; then
                    tput rc
                    tput ed
                    echo "======================================================================================="
            fi
            while read p || [[ -n $p ]]; do
                    hostname=$(echo $p | cut -d';' -f1)
                    type=$(echo $p | cut -d';' -f2)
                    if [[ "$type" = "$ENVIR" || "$ENVIR" = "ALL" || "$hostname" = "$ENVIR" ]]
    then
                            if [ $lastNbLinesTmp != $lastNbLines ]; then
                                    echo -e "${cBold}Server $hostname :${cNone}"
                                    cat "$hostname.status.tmp"
                            fi
                    fi
            done < $OAAPATH/hostnames.prm
            processing=0
            for actionPid in "${actionPids[@]}"
            do
            if kill -0 $actionPid 2>/dev/null ; then
                    processing=1
            fi
            done
            **sleep 1**
            lastNbLines=$lastNbLinesTmp
    done
    
    # Wait for all process to finish before exit
    wait
    rm *.status.tmp
    
    [ $RET_SUM -ne 0 ] && exit 1
    exit 0
    

    效果很好。

    这有点复杂,但我认为没有真正最简单的解决方案。

    【讨论】:

    • 这个伪代码实际上不是 bash 语法。请尽量确保您的答案(甚至是您自己的答案!)写得足够详细,以便对可能有类似问题的其他人有所帮助。
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多