我正在尝试编写一个简单的脚本来持续监视某个进程的内存使用情况。所以我想尝试使用' top'使用grep命令捕获某个进程的内存使用情况,以及运行free -m命令并将其输出写入文本文件。
top | grep --line-buffered -i process_name >> /home/output_txt1 &
while (1)
free -m >> /home/output_txt2
sleep 2
end &
然而,当我运行命令时,我得到了
- 暂停(tty输出)top | grep --line-buffered -i process_name>> / home / output_txt1&
我做错了什么,如何实现我想要的?知道我也尝试过使用' watch'在使用while循环之前,它也没有用。
答案 0 :(得分:0)
正如我在评论中所说,来自man top
:
-b:批处理模式操作 在“模式”中开始,这对于将输出从顶部发送到其他程序或文件非常有用。在这种模式下,top将不接受输入并运行,直到迭代限制你使用â-n'命令行选项设置或直到被杀死。
还有:
-n:迭代次数限制为:-n number 指定在结束之前应生成的最大迭代次数或帧数。
我不明白你想要做什么,但你应该检查下面的脚本是否满足你的要求:
#!/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]
希望它有所帮助。