2017-09-06 90 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命令從未被執行過。 –