正如我在评论说,从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]
希望它能帮助。
从'man top': -b:批处理模式操作 在“批处理模式”中开始顶部,这对从顶部向其他程序或文件发送输出可能有用。 在这种模式下,顶部将不会接受输入并运行,直到迭代限制您设置了'-n'命令行选项或直到终止。 您可以尝试使用-b选项。 –
感谢您的提示,我用-b和现在; top命令无错误地工作。 但是,我的脚本没有发生什么,我发现它的连续运行的顶层命令并正确写入文件,而“free -m”命令只运行一次。是不是像“top”命令劫持进程并且不允许再次执行“while”循环? –