Bash 关注多进程 returns
Bash follow multi process returns
我有一个 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" 脚本在每台服务器上需要一些时间才能启动。
它 returns 逐步说明正在发生的事情。
我想逐步显示每个进程的每个动作的状态,刷新屏幕。
示例:
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
所以我按主机名将进度步骤 return 存储在一个文件中:
ssh "$hostname" "someAction..." > "$hostname.status.tmp" &
但是,当至少有一个进程在进行时,我该如何显示它们呢?
似乎很难:/
感谢您的回答。
我只是用:
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 启动多进程的脚本:
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" 脚本在每台服务器上需要一些时间才能启动。 它 returns 逐步说明正在发生的事情。
我想逐步显示每个进程的每个动作的状态,刷新屏幕。
示例:
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
所以我按主机名将进度步骤 return 存储在一个文件中:
ssh "$hostname" "someAction..." > "$hostname.status.tmp" &
但是,当至少有一个进程在进行时,我该如何显示它们呢?
似乎很难:/
感谢您的回答。
我只是用:
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
它完美运行。
有点复杂,但我认为没有真正最简单的解决方案。