我已经在我们的主要脚本之一中并行实现了服务器之间的数据迁移。目前,输出以漂亮的颜色同时全部显示(-u),并根据正在运行的序列(例如5/20: $username: rsyncing homedir或5/20: $username: restoring account)执行功能的状态周期性回显。这些都直接回显到运行脚本的终端,并在那里积累。但是,根据命令运行的时间长短,输出最终rsync可能会混乱不堪,长时间运行的命令可能会在随机播放中丢失。但是,我不想等待长时间运行的进程完成才能获得后续进程的输出。
简而言之,我的问题是跟踪正在处理哪些参数并仍在运行。
我想做的是与并行发送到后台,(parallel args command {#} {} ::: $userlist) &然后跟踪每个正在运行的功能的进度。我最初的想法是每隔几秒钟就使用ps并grep随意地tput重写屏幕。我通常并行运行三个作业,因此我想要一个显示例如以下内容的屏幕:
1/20: user1: syncing homedir
current file: /home/user1/www/cache/file12589015.php
12/20: user12: syncing homedir
current file: /home/user12/mail/joe/mailfile
5/20: user5: collecting information
current file:
Run Code Online (Sandbox Code Playgroud)
我当然可以将上述状态输出汇总在一起没问题,但是我目前的麻烦是将输出从各个并行进程中分离成三个不同的管道。变量?文件?以便可以将其解析为以上信息。
我相信这接近我所需要的,尽管它不是很整洁并且可能不是最佳的:
#!/bin/bash
background() { #dummy load. $1 is text, $2 is number, $3 is position
echo $3: starting sleep...
sleep $2
echo $3: $1 slept for $2
}
progress() {
echo starting progress loop for pid $1...
while [ -d /proc/$1 ]; do
clear
tput cup 0 0
runningprocs=`ps faux | grep background | egrep -v '(parallel|grep)'`
numprocs=`echo "$runningprocs" | wc -l`
for each in `seq 1 ${numprocs}`; do
line=`echo "$runningprocs" | head -n${each} | tail -n1`
seq=`echo $line | rev | awk '{print $3}' | rev`
# print select elements from the ps output
echo working on `echo $line | rev | awk '{print $3, $4, $5}' | rev`
# print the last line of the log for that sequence number
cat logfile.log | grep ^$seq\: | tail -n1
echo
done
sleep 1
done
}
echo hello im starting now
sleep 1
export -f background
# start parallel and send the job to the background
parallel -u -j3 background {} {#} '>>' logfile.log ::: foo bar baz foo bar baz one two three one two three :::+ 5 6 5 3 4 6 7 2 5 4 6 2 &
pid=$!
progress $pid
echo finished!
Run Code Online (Sandbox Code Playgroud)
我宁愿不依赖于从中抓取所有信息ps,而是更愿意获得每个并行进程的实际行输出,但是一个人必须做他必须做的事情。常规输出发送到日志文件以供稍后解析。
| 归档时间: |
|
| 查看次数: |
909 次 |
| 最近记录: |