GNU/Linux >> Linux の 問題 >  >> Linux

Unix のトップ コマンド出力を読み取り可能な形式でファイルにキャプチャする方法

質問: top コマンドの出力を取得しようとしています ファイルに。 top> output.txt を実行すると、output.txt ファイルに大量のジャンク文字が含まれています。 top コマンドの出力を読み取り可能なテキスト ファイルにキャプチャする最良の方法は何ですか?

答え: top コマンドのバッチ モード操作オプション ( -b ) を使用して、top コマンドの出力をファイルにキャプチャします。

以下に示すように、トップ コマンドの出力をテキスト ファイルにリダイレクトしようとすると、出力ファイルに大量のジャンク文字が含まれていることに気付くでしょう。

less コマンドを使用して出力ファイルを表示しようとすると、出力ファイルが大量のジャンク文字で作成されていることに気付くでしょう。

$ top -n 1 > top-output.txt

$ less top-output.txt
"top-output.txt" may be a binary file.  See it anyway? 

注: オプション -n 1 は、トップ コマンドの反復を 1 回だけ実行する必要があることを示します。

この問題を回避し、読みやすい top コマンド出力を取得するには、top コマンドでオプション -b を使用します。以下に示すように、トップ コマンドをバッチ モードで実行します。

$ top -n 1 -b > top-output.txt

$ less top-output.txt
top - 16:56:36 up 246 days, 11:14,  3 users,  load average: 0.00, 0.00, 0.00
Tasks: 168 total,   1 running, 167 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   8306856k total,  7940744k used,   366112k free,   285136k buffers
Swap:  8385920k total,      104k used,  8385816k free,  7391824k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
    1 root      15   0  2064  592  512 S  0.0  0.0   0:02.24 init               
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.47 migration/0        
    3 root      35  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0        
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0         
    5 root      RT  -5     0    0    0 S  0.0  0.0   0:00.61 migration/1       

トップ コマンドのマニュアル ページのオプション -b および -n について:

       -b : Batch mode operation
            Starts top in "Batch mode", which could be useful for sending out-
            put from top to other programs or to a file.  In  this  mode,  top
            will  not  accept input and runs until the iterations limit youâve
            set with the â-nâ command-line option or until killed.

       -n : Number of iterations limit as:  -n number
            Specifies the maximum number of iterations, or frames, top  should
            produce before ending.

以下に示すように、このメソッドを使用して、top コマンドの出力を別のプログラムにリダイレクトすることもできます。

$ top -n1 -b | head
top - 16:58:36 up 246 days, 11:14,  3 users,  load average: 0.00, 0.00, 0.00
Tasks: 169 total,   1 running, 168 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   8306856k total,  7941612k used,   365244k free,   285144k buffers
Swap:  8385920k total,      104k used,  8385816k free,  7392088k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
    1 root      15   0  2064  592  512 S  0.0  0.0   0:02.24 init               
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.47 migration/0        
    3 root      39  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0        

Linux
  1. Unix Stat コマンド:ファイル属性を識別する方法

  2. Unix Vmstat コマンド出力にタイムスタンプを追加する方法

  3. 12 UNIX / Linux 時刻コマンド出力形式オプション例

  1. Linuxでコマンド出力をファイルに保存する方法

  2. トップコマンドの出力をLinuxのファイルにキャプチャする方法は?

  3. トップコマンドで出力をラップするにはどうすればよいですか?

  1. コマンド出力またはファイルの内容を列形式で表示する

  2. Linuxスクリプトコマンドを使用してターミナルセッションをキャプチャして出力する方法

  3. Unix で ls コマンドからファイル サイズ/詳細を表示する方法