Shell脚本中的top命令 [英] top command in shell script

查看:625
本文介绍了Shell脚本中的top命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Expect脚本获得top命令的前5行.我从外壳程序脚本中调用了这个期望脚本以及其他一些内容.

I am trying to get first 5 lines of top command through expect script. Im calling this expect script from a shell script along with some other stuff.

top | head -5给我下面的输出,即没有cpu统计信息-

top | head -5 gives me below output ie without cpu stats-

顶部-09:10:58最多46天,17:03、12个用户,平均负载:0.01、0.02, 0.00个任务:总共138个,正在运行1个,在睡眠137个,停止0个,僵尸0个

top - 09:10:58 up 46 days, 17:03, 12 users, load average: 0.01, 0.02, 0.00 Tasks: 138 total, 1 running, 137 sleeping, 0 stopped, 0 zombie

Mem:总计16432400k,使用的8408096k,免费的8024304k,609200k 缓冲区交换:总计6290736k,已使用0k,免费6290736k, 6754356k已缓存

Mem: 16432400k total, 8408096k used, 8024304k free, 609200k buffers Swap: 6290736k total, 0k used, 6290736k free, 6754356k cached

如果我仅在该远程服务器上运行top命令,我可以看到在更新CPU状态行之前有2-3秒的延迟,请问有人可以帮助我获得所有5条更新了CPU状态的行吗?以下是我的期望脚本-

If I run just top command on that remote server I can see there is a 2-3 second delay before the CPU states line is updated, can some one please help me to get all 5 lines with updated cpu states? Below is my expect script -

#!/usr/bin/expect -f
set user1 abc
set pass1 pass
set timeout 8
match_max 1000
spawn ssh -C -o stricthostkeychecking=no $user1@<ip>
expect "*?assword:*"
send  "$pass3\r"
expect "?xterm*"
send "\r"
send "top | head -5\r"
expect eof

推荐答案

您需要在batch mode中运行top,而不是默认的interactive mode.另外,您需要定义top为获取其测量值而执行的迭代次数.

You need to run top in batch mode instead of the default interactive mode. In addition you need to define the number of iterations that top performs for getting its measurements.

num_iterations=3
top -b -n $num_iterations | head -5

如果您希望输出仅列出前5个进程并跳过显示的统计信息标题,则可以尝试以下操作:

If you want the output to only list the top 5 processes and skip the statistic header displayed, you can try this:

num_iterations=3
top -b -n $num_iterations | sed -n '8,12p'

还可以根据需要调整num_iterations的值.

Also tune the value of num_iterations as per your need.

这篇关于Shell脚本中的top命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆