#!/bin/bash
# Start.sh
if [[ $# -ne 1 ]] ; then
echo 'Usage :<./Start N# >'
exit 1
fi
if [[ $1 -ge 1 ]] ; then
for (( c=1; c<=$1; c++ ))
do
virsh start VM$c
/usr/bin/time -f "%E" ./test-online.sh VM$c &
done
else
echo 'Give some positive number !!!'
fi
/usr/bin/iostat -x 1 > result.txt
在上面的脚本中,我测量了 VM 启动的时间,它写在 test-online.sh 脚本中。我想在每个后台进程 (test-online.sh) 完成时停止/usr/bin/iostat -x 1 > result.txt 命令。
如果我们添加“wait”命令而不是“/usr/bin/iostat -x 1 > result.txt”,Start.sh 将等待后台进程。但是现在我很困惑,怎么办?
谢谢。
最佳答案
我曾经遇到过这个问题。因为我是 Python 的忠实粉丝,所以我在 Python 中实现了一个模块:https://gist.github.com/myaut/38e5d7cb813ed0db379c
这是一个小例子:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, subprocess
import time
import bench
from datetime import datetime
cmd = "sleep 40"
benchdir = '/var/tmp/mybench-%s/' % datetime.now().strftime('%Y.%m.%d.%H.%M')
print 'Destination is %s' % benchdir
stats = [bench.statcmd('date', [], 5, False),
bench.statcmd('vmstat', ['5']),
]
bench.run_test(cmd, stats, 'sleep', benchdir)
模块还支持 Python 类作为统计收集器,但我在示例中省略了它。此示例每 5 秒运行一次 date
命令,并以 5
作为参数连续运行 vmstat
。输出写入 /var/tmp/mybench-DATE
目录。
再次为非 bash 实现感到抱歉。希望对您有所帮助。
关于linux - 如何运行 iostat/vmstat/top/sar 直到所有后台进程完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28828470/