2017-09-06 88 views
0

我正在尝试编写一个连续监视某个进程的内存使用情况的简单脚本。所以我试图用'top'和grep命令来捕获特定进程的内存使用情况,以及运行free -m命令并将其输出写入文本文件。并行运行top和free-m命令

top | grep --line-buffered -i process_name >> /home/output_txt1 & 
while (1) 
    free -m >> /home/output_txt2 
    sleep 2 
end & 

然而,当我运行的命令,我得到

  • 暂停(TTY输出)顶部| grep的--line缓冲-i PROCESS_NAME >> /家庭/ output_txt1 &

什么我做错了,我怎么能实现我想要什么?知道我在使用while循环之前也尝试过使用'watch',并且它也不起作用。

+0

从'man top': -b:批处理模式操作 在“批处理模式”中开始顶部,这对从顶部向其他程序或文件发送输出可能有用。 在这种模式下,顶部将不会接受输入并运行,直到迭代限制您设置了'-n'命令行选项或直到终止。 您可以尝试使用-b选项。 –

+0

感谢您的提示,我用-b和现在; top命令无错误地工作。 但是,我的脚本没有发生什么,我发现它的连续运行的顶层命令并正确写入文件,而“free -m”命令只运行一次。是不是像“top”命令劫持进程并且不允许再次执行“while”循环? –

回答

0

正如我在评论说,从man top

-b:批处理模式操作顶部 开始在âBatchmodeâ,这可能是从顶部输出发送到其他程序或文件非常有用。在这种模式下,顶层将不会接受输入并运行,直到迭代限制您使用“n”命令行选项设置或直到死亡。

也:

-n:迭代次数限制为:-n数 指定的迭代,或帧的最大数量,顶部应当结束之前产生。

我不明白,正是你想做的事,但你应该检查一下下面的脚本fullfill您的要求:

#!/bin/bash 

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 
OUT_TOP="${DIR}/out_top" 
OUT_FREE="${DIR}/out_free" 
echo "$(date) Reset" > "${OUT_TOP}" 
echo "$(date) Reset" > "${OUT_FREE}" 

while true 
do 
    date >> "${OUT_TOP}" 
    date >> "${OUT_FREE}" 
    top -b -n 1 | grep --line-buffered -i apache >> "${OUT_TOP}" 
    free -m >> "${OUT_FREE}" 
    sleep 2 
done 

无限循环是从here到来。

当我测试,它给了接近你可能会搜索结果:

[so46072643] ./topandfree & 
[1] 27313 
[so46072643] sleep 8 
[so46072643] kill 27313 
[so46072643] for f in `ls ./out*`; do printf "\n%s\n<<--\n" $f; cat $f; echo "-->>"; done 

./out_free 
<<-- 
Wed Sep 6 12:54:54 CEST 2017 Reset 
Wed Sep 6 12:54:54 CEST 2017 
      total  used  free  shared buffers  cached 
Mem:   7985  6808  1177   0  263  1400 
-/+ buffers/cache:  5144  2840 
Swap:   8031  117  7914 
Wed Sep 6 12:54:56 CEST 2017 
      total  used  free  shared buffers  cached 
Mem:   7985  6808  1176   0  263  1400 
-/+ buffers/cache:  5145  2840 
Swap:   8031  117  7914 
Wed Sep 6 12:54:59 CEST 2017 
      total  used  free  shared buffers  cached 
Mem:   7985  6808  1176   0  263  1400 
-/+ buffers/cache:  5145  2840 
Swap:   8031  117  7914 
Wed Sep 6 12:55:01 CEST 2017 
      total  used  free  shared buffers  cached 
Mem:   7985  6808  1176   0  263  1400 
-/+ buffers/cache:  5144  2840 
Swap:   8031  117  7914 
Wed Sep 6 12:55:04 CEST 2017 
      total  used  free  shared buffers  cached 
Mem:   7985  6808  1177   0  263  1400 
-/+ buffers/cache:  5144  2840 
Swap:   8031  117  7914 
Wed Sep 6 12:55:06 CEST 2017 
      total  used  free  shared buffers  cached 
Mem:   7985  6808  1177   0  263  1400 
-/+ buffers/cache:  5144  2841 
Swap:   8031  117  7914 
[1]+ Terminated    ./topandfree 
-->> 

./out_top 
<<-- 
Wed Sep 6 12:54:54 CEST 2017 Reset 
Wed Sep 6 12:54:54 CEST 2017 
4747 apache 20 0 171m 2068 436 S 0.0 0.0 0:00.00 httpd 
4748 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4750 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4751 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4752 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4753 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4754 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4755 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
Wed Sep 6 12:54:56 CEST 2017 
4747 apache 20 0 171m 2068 436 S 0.0 0.0 0:00.00 httpd 
4748 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4750 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4751 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4752 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4753 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4754 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4755 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
Wed Sep 6 12:54:59 CEST 2017 
4747 apache 20 0 171m 2068 436 S 0.0 0.0 0:00.00 httpd 
4748 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4750 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4751 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4752 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4753 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4754 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4755 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
Wed Sep 6 12:55:01 CEST 2017 
4747 apache 20 0 171m 2068 436 S 0.0 0.0 0:00.00 httpd 
4748 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4750 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4751 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4752 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4753 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4754 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4755 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
Wed Sep 6 12:55:04 CEST 2017 
4747 apache 20 0 171m 2068 436 S 0.0 0.0 0:00.00 httpd 
4748 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4750 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4751 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4752 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4753 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4754 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4755 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
Wed Sep 6 12:55:06 CEST 2017 
4747 apache 20 0 171m 2068 436 S 0.0 0.0 0:00.00 httpd 
4748 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4750 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4751 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4752 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4753 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4754 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
4755 apache 20 0 171m 2072 440 S 0.0 0.0 0:00.00 httpd 
-->> 
[so46072643] 

希望它能帮助。

+0

是的,非常感谢。 我的错误是我使用“-b”而未使用“-n”参数。所以,顶级命令劫持了进程并没有结束,所以free -m命令从未被执行过。 –