如何等待命令的特定输出字符串并执行另一个命令

时间:2016-04-08 01:58:30

标签: linux shell

我有一个这样的应用程序:

filebench -f fileserver.f

输出为:

Filebench Version 1.4.9.1
WARNING: Could not open /proc/sys/kernel/shmmax file!
It means that you probably ran Filebench not as a root. Filebench will not increase shared
region limits in this case, which can lead to the failures on certain workloads.
 3297: 0.000: Allocated 170MB of shared memory
 3297: 0.001: File-server Version 3.0 personality successfully loaded
 3297: 0.001: Creating/pre-allocating files and filesets
 3297: 0.007: Fileset bigfileset: 10000 files, 0 leafdirs, avg dir width = 20, avg dir depth = 3.1, 1240.757MB
 3297: 0.009: Removed any existing fileset bigfileset in 1 seconds
 3297: 0.009: making tree for filset /home/njupudi/workloads/bigfileset
 3297: 0.014: Creating fileset bigfileset...
 3297: 6.795: Preallocated 7979 of 10000 of fileset bigfileset in 7 seconds
 3297: 6.795: waiting for fileset pre-allocation to finish
 3301: 6.795: Starting 1 filereader instances
 3302: 6.796: Starting 50 filereaderthread threads
 3297: 8.183: Running...
 3297: 13.184: Run took 5 seconds...
 3297: 13.279: Per-Operation Breakdown
statfile1            4674ops      918ops/s   0.0mb/s      0.0ms/op     1575us/op-cpu [0ms - 0ms]
deletefile1          4674ops      918ops/s   0.0mb/s     12.0ms/op    13256us/op-cpu [0ms - 1170ms]
closefile3           4697ops      922ops/s   0.0mb/s      0.0ms/op     1601us/op-cpu [0ms - 0ms]
readfile1            4697ops      922ops/s 113.7mb/s      0.0ms/op     1720us/op-cpu [0ms - 0ms]
openfile2            4701ops      923ops/s   0.0mb/s      0.0ms/op     1778us/op-cpu [0ms - 20ms]
closefile2           4701ops      923ops/s   0.0mb/s      0.0ms/op     1544us/op-cpu [0ms - 0ms]
appendfilerand1      4701ops      923ops/s   7.3mb/s      1.4ms/op     2314us/op-cpu [0ms - 203ms]
openfile1            4714ops      926ops/s   0.0mb/s      0.0ms/op     1773us/op-cpu [0ms - 21ms]
closefile1           4714ops      926ops/s   0.0mb/s      0.0ms/op     1597us/op-cpu [0ms - 0ms]
wrtfile1             4714ops      926ops/s 116.4mb/s     21.4ms/op    10013us/op-cpu [0ms - 658ms]
createfile1          4724ops      928ops/s   0.0mb/s      0.1ms/op     1897us/op-cpu [0ms - 39ms]
 3297: 13.279: IO Summary: 51711 ops, 10153.731 ops/s, (922/1849 r/w), 237.8mb/s,    337us cpu/op,  11.6ms latency
 3297: 13.279: Shutting down processes

这里初始化,应用程序大约需要2-3秒,然后开始运行。我们可以在输出中看到如下:

3297: 8.183: Running...

此后,应用程序开始运行指定的时间。同时,我想在控制台上看到“正在运行...”时启动另一个应用程序(更确切地说是一个shell脚本)。有没有办法捕获控制台的输出并将其与“正在运行...”进行比较,然后在此命令运行时启动shell脚本?

1 个答案:

答案 0 :(得分:1)

这是@Rilwan的评论应用于您可以使用的脚本。假设您有一个名为myrunningfilter的脚本:

#!/bin/bash

while read -r line; do
  echo $line
  if echo $line | grep -q ': Running...$'; then
    <run your script here>
  fi
done

您可以通过管道应用程序的输出来运行命令:

filebench -f fileserver.f | ./myrunningfilter

或者如果filebench在日志文件上产生输出:

tail -F /var/log/filebench.log | ./myrunningfilter