「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.

答え2

Windowsシステムで同様の型番号を取得するには、powershellを調べる必要があります。

プロセスのリストを入手して確認してくださいget-process。確認してみてください引用する

追加検索で素敵な小さなコマンドを見つけました。ここ

提案されたwhileループを使用すると、要件を満たすことは次のとおりです。

ps | sort -desc cpu | select -first 30

pspowershellのエイリアスですget-process

答え3

macOSハッカー

申し訳ありません。不便ではありませんが、macOSで私が欲しいものを手に入れました。

top -l 2 -n 10 | tail -22
  • -l 2- コメントに応じてサンプルサイズのみが与えられると、macosはCPUフィールドに0を出力します。したがって、2回サンプリングしてください。

  • -n 10最上位プロセスのみを使用する(CPUロード基準がデフォルトの順序である)

  • tail -22 - 実際のハッキング部分 - 最初の例の行はスキップしてください。-22この場合はすべて試行錯誤でした-n 10

あなたはこれを誇りに思っていますか?決してそうではありませんが、おおよそ私が望むことを行います。

Processes: 547 total, 3 running, 544 sleeping, 2558 threads 
2022/11/24 17:18:44
Load Avg: 1.74, 2.05, 2.01 
CPU usage: 6.25% user, 1.71% sys, 92.2% idle 
SharedLibs: 257M resident, 35M data, 14M linkedit.
MemRegions: 229362 total, 3865M resident, 114M private, 1192M shared.
PhysMem: 16G used (3357M wired), 143M unused.
VM: 11T vsize, 2317M framework vsize, 11336543(0) swapins, 13536939(0) swapouts.
Networks: packets: 33586023/7445M in, 34143918/6707M out.
Disks: 9964939/225G read, 3781186/116G written.

PID    COMMAND          %CPU TIME     #TH    #WQ #PORTS MEM    PURG CMPRS PGRP  PPID  STATE    BOOSTS   %CPU_ME %CPU_OTHRS UID FAULTS    COW   MSGSENT    MSGRECV    SYSBSD     SYSMACH    CSW        PAGEINS IDLEW     POWER INSTRS     CYCLES     USER          #MREGS RPRVT VPRVT VSIZE KPRVT KSHRD
17013  postgres         28.5 01:00.77 1      0   8      62M+   0B   0B    17013 16225 sleeping *0[1]    0.00000 0.00000    501 545151+   124   152        5          353119+    118842+    11731+     0       0         28.5  3760288663 1617021064 postgres      N/A    N/A   N/A   N/A   N/A   N/A  
17019  postgres         25.9 01:04.16 1/1    0   8      64M+   0B   0B    17019 16225 running  *0[1]    0.00000 0.00000    501 552890+   123   160+       5          359735+    120494+    11930+     0       0         25.9  3263636650 1446362124 postgres      N/A    N/A   N/A   N/A   N/A   N/A  
17329  top              8.8  00:00.52 1/1    0   28+    3728K+ 0B   0B    17329 13212 running  *0[1]    0.03698 0.00000    0   2471+     106+  517539+    255643+    4594+      264177+    48+        0       0         8.8   388290277  451214497  root          N/A    N/A   N/A   N/A   N/A   N/A  
16245  Python           8.3  08:43.72 1      0   21     1027M+ 0B   1104K 16245 37017 sleeping *0[1]    0.00000 0.00000    505 1008120+  3429  3229       41         8420748+   6791       2448946+   87      35        8.3   442524899  451503337  me__          N/A    N/A   N/A   N/A   N/A   N/A  
17015  postgres         5.1  00:12.19 1      0   8      29M+   0B   0B    17015 16225 sleeping *0[1]    0.00000 0.00000    501 8276+     114   14         5          360531+    30         85127+     0       5         5.1   516698298  269517335  postgres      N/A    N/A   N/A   N/A   N/A   N/A  
17020  postgres         4.8  00:12.64 1      0   8      32M+   0B   0B    17020 16225 sleeping *0[1]    0.00000 0.00000    501 9074+     113   14         5          400661+    34         94522+     0       6         4.8   486455789  262738456  postgres      N/A    N/A   N/A   N/A   N/A   N/A  
0      kernel_task      3.7  01:46:28 287/16 0   0      1436M  0B   0B    0     0     running   0[0]    0.00000 0.00000    0   6106435   9589  331617464+ 300334081+ 0          0          615407045+ 183     32498664+ 0.0   96723302   461786276  root          N/A    N/A   N/A   N/A   N/A   N/A  
15976  Python           3.0  01:39.90 2      0   24     90M    0B   4860K 94785 94785 sleeping *0[1]    0.00000 0.00000    505 26811     643   460        21         6928600+   1008       28526+     448     1886+     3.1   235167151  177692860  me__          N/A    N/A   N/A   N/A   N/A   N/A  
16226  postgres         1.6  00:39.95 1      0   8      728K   0B   188K  16226 16225 sleeping *0[1]    0.00000 0.00000    501 340       73    23         5          10634810+  20         2383516+   0       4         1.6   37954507   77088931   postgres      N/A    N/A   N/A   N/A   N/A   N/A  
150    WindowServer     1.4  04:23:51 16     7   2944   1307M- 19M  107M  150   1     sleeping *0[1]    0.02412 0.01154    88  15718938+ 72229 255258396+ 99186014+  280197394+ 409489438+ 118640818+ 3284    3028879   1.4   29648136   64463377   _windowserver N/A    N/A   N/A   N/A   N/A   N/A  

関連情報