如何一起运行两个命令?

时间:2015-09-05 20:01:08

标签: linux shell

我编写了一个shell脚本(myScript.sh),其中包含以下格式的一组命令:

command1 #first run
command1 #second run
command1 #third run

现在我想找到每次运行command1所消耗的最大RAM。通过在a中运行,我可以找到命令1消耗的最大RAM 单独终止命令:

smem -c "command pss" | grep "command1"

为了找到第一次,第二次和第三次单独运行所消耗的最大RAM,我以交错的方式手动运行命令1和涂抹在不同的终端上

command1 #terminal 1
smem -c "command pss" | grep "command1" #terminal 2 in a loop to get maximum memory
command1 #terminal 1
smem -c "command pss" | grep "command1" #terminal 2 in a loop to get maximum memory
command1 #terminal 1
smem -c "command pss" | grep "command1" #terminal 2 in a loop to get maximum memory/

有没有什么方法可以使用linux命令或python在单个shell脚本或程序中自动执行此过程?

1 个答案:

答案 0 :(得分:1)

不确定Linux,但您可以在OS X下的Mac上执行此操作:

/usr/bin/time -l <command> 2>&1 | grep -i max

示例输出:

/usr/bin/time -l sleep 1 2>&1 | grep -i max
557056  maximum resident set size                 # <--- program used 557kB

或,完整输出:

/usr/bin/time -l sleep 1
        1.00 real         0.00 user         0.00 sys
    557056  maximum resident set size
         0  average shared memory size
         0  average unshared data size
         0  average unshared stack size
       145  page reclaims
         0  page faults
         0  swaps
         0  block input operations
         0  block output operations
         0  messages sent
         0  messages received
         0  signals received
         0  voluntary context switches
         2  involuntary context switches