Top
如何將頂部快照流式傳輸到文件中?
有沒有辦法
top
將給定時刻的快照保存到文件中?我試過
top > top.out
但內容與 STDOUT 中的頂部完全不同:
^[[H^[[2J^[(B^[[mtop - 16:09:49 up 18 days, 21:46, 4 users, load average: 2.00, 1.90, 1.87^[(B^[[m^[[39;49m^[[K Tasks:^[(B^[[m^[[39;49m^[(B^[[m 286 ^[(B^[[m^[[39;49mtotal,^[(B^[[m^[[39;49m^[(B^[[m 1 ^[(B^[[m^[[39;49mrunning,^[(B^[[m^[[39;49m^[(B^[[m 285 ^[(B^[[m^[[39;49msleeping,^[(B^[[m^[[39;49m^[(B^[[m 0 ^[(B^[[m^[[39;49mstopped,^[(B^[[m^[[39;49m^[(B^[[m 0 ^[(B^[[m^[[39;49mzombie^[(B^[[m^[[39;49m^[[K Cpu(s):^[(B^[[m^[[39;49m^[(B^[[m 5.9%^[(B^[[m^[[39;49mus,^[(B^[[m^[[39;49m^[(B^[[m 0.1%^[(B^[[m^[[39;49msy,^[(B^[[m^[[39;49m^[(B^[[m 0.0%^[(B^[[m^[[39;49mni,^[(B^[[m^[[39;49m^[(B^[[m 93.5%^[(B^[[m^[[39;49mid,^[(B^[[m^[[39;49m^[(B^[[m 0.3%^[(B^[[m^[[39;49mwa,^[(B^[[m^[[39;49m^[(B^[[m 0.0%^[(B^[[m^[[39;49mhi,^[(B^[[m^[[39;49m^[(B^[[m 0.1%^[(B^[[m^[[39;49msi,^[(B^[[m^[[39;49m^[(B^[[m 0.0%^[(B^[[m^[[39;49mst^[(B^[[m^[[39;49m^[[K Mem: ^[(B^[[m^[[39;49m^[(B^[[m 64322524k ^[(B^[[m^[[39;49mtotal,^[(B^[[m^[[39;49m^[(B^[[m 46529492k ^[(B^[[m^[[39;49mused,^[(B^[[m^[[39;49m^[(B^[[m 17793032k ^[(B^[[m^[[39;49mfree,^[(B^[[m^[[39;49m^[(B^[[m 141304k ^[(B^[[m^[[39;49mbuffers^[(B^[[m^[[39;49m^[[K Swap:^[(B^[[m^[[39;49m^[(B^[[m 6289436k ^[(B^[[m^[[39;49mtotal,^[(B^[[m^[[39;49m^[(B^[[m 696k ^[(B^[[m^[[39;49mused,^[(B^[[m^[[39;49m^[(B^[[m 6288740k ^[(B^[[m^[[39;49mfree,^[(B^[[m^[[39;49m^[(B^[[m 15289220k ^[(B^[[m^[[39;49mcached^[(B^[[m^[[39;49m^[[K ^[[6;1H
問題是
top
預設情況下包括 ANSI/VT100 轉義碼。在這種情況下,您想要的似乎只是“原始”文本輸出(即不是某些文本應該是粗體等的資訊等)。幸運的是,top
它內置了對這種操作模式的支持,在他們的文件中稱為“批處理模式”。從頂部手冊頁 (
man top
):-n : Number of iterations limit as: -n number Specifies the maximum number of iterations, or frames, top should produce before ending. -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.
所以…
top -n 1 -b > top.out
對於沒有像 top 這樣方便的“-b”類型開關的其他命令行實用程序,也可以使用ansifilter來完成。下載、編譯和安裝。那麼你可以…
top -n 1 | ansifilter > top.out