【问题标题】:Running top and free-m commands in parallel并行运行 top 和 free-m 命令
【发布时间】:2017-09-06 10:16:40
【问题描述】:

我正在尝试编写一个简单的脚本来持续监控某个进程的内存使用情况。所以我尝试使用 '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 输出)顶部 | grep --line-buffered -i process_name >> /home/output_txt1 &

我做错了什么,如何实现我想要的?知道我在使用 while 循环之前也尝试过使用“watch”,但它也不起作用。

【问题讨论】:

  • 来自man top: -b : 批处理模式操作 以“批处理模式”启动 top,这对于将输出从 top 发送到其他程序或文件可能很有用。在这种模式下,top 将不接受输入并运行,直到您使用 '-n' 命令行选项设置的迭代限制或直到被杀死。你可以试试 -b 选项。
  • 感谢您的提示,我现在使用了 -b; top 命令可以正常工作。但是,我从脚本中想要的并没有发生,我发现它持续运行的 top 命令并正确写入文件,而“free -m”命令只运行一次。是不是类似于“top”命令劫持了进程,不允许“while”循环再次执行?

标签: linux memory process


【解决方案1】:

正如我在评论中所说,来自man top

-b : 批处理模式操作 在“批处理模式”中启动 top,这对于将输出从 top 发送到其他程序或文件可能很有用。在这种模式下,top 将不接受输入并运行,直到您使用â-nâ 命令行选项设置的迭代限制或直到被终止。

还有:

-n :迭代次数限制为:-n number 指定 top 在结束前应该产生的最大迭代次数或帧数。

我不完全了解您想要做什么,但您应该检查以下脚本是否满足您的要求:

#!/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]

希望对你有帮助。

【讨论】:

  • 是的,它有效,非常感谢。我犯的错误是我使用“-b”而不使用“-n”参数。所以,top命令劫持了进程并没有结束,所以free -m命令从未执行过。
猜你喜欢
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-08
  • 1970-01-01
  • 2013-04-11
相关资源
最近更新 更多