linux - 如何运行 iostat/vmstat/top/sar 直到所有后台进程完成?

标签 linux bash scripting ubuntu-12.04 benchmarking

#!/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/

相关文章:

c++ - 监控表中的数据

linux - 刷新页面时出现数据库被锁定的错误

linux - 表达式 : non-integer argument error in Cygwin

bash 非数组 ${!name[@]} 参数扩展困惑

php - 一个 PHP 文件可以处理多个请求吗?

scripting - 如何在 vimscript 中与外部命令交互?

linux - 针对 Active Directory 验证登录凭据(适用于受工作站限制的用户)

linux - 将本地 repo 移动到远程单个文件

linux - 从命令行乘以 .dat 文件中的常数

bash - Windows 2008 R2 x64上的Hadoop 2.3.0上的Pig 0.12.0