「top」を一度実行して終了する方法はありますか?

「top」を一度実行して終了する方法はありますか?

私はシェルコマンドの出力がtop私のコンピュータで何が起こっているのか、おおよそのアイデアを得る簡単でおなじみの方法だと思います。topおおよそのモニタリングのために、コンピュータの小さなWebサーバー(またはそれに非常に似ているもの)の出力を提供したいと思います。

テキスト出力を正確に書く方法はありますか?一度、書式設定文字がありませんか?私はこれを試しました:

(sleep 1; echo 'q') | top > output.txt

そうだ閉鎖(1)1画面分の情報を取得できないという保証はありません。

topまたは、システム全体とプロセスレベルのメモリ/ CPU使用量/稼働時間情報を一覧表示する他の同様のコマンドがありますか?

(理想的には、私たちの開発者はMacを使用し、本番環境はLinuxなので、LinuxとMac OS Xに移植可能な戦略をしたいと思います。)

ベストアンサー1

Linuxでは、次のことを試すことができます。

top -bn1 > output.txt

からman top

-b : Batch-mode operation
            Starts top in 'Batch' mode, which could be useful for sending
            output 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.

OS Xの場合は、以下を試してください。

top -l 1

~から親OSXのマンページ:

 -l <samples>
              Use logging mode and display <samples> samples, even if 
              standard output is a terminal. 0 is treated  as  infinity.   
              Rather than redisplaying, output is periodically printed in 
              raw form. Note that the first sample displayed will have an 
              invalid %CPU displayed for each process,  as it is calculated 
              using the delta between samples.

おすすめ記事