Bagaimana cara streaming snapshot atas ke file?

15

Apakah ada cara untuk mempertahankan snapshot toppada saat itu ke dalam file?

Saya mencoba

top > top.out

tetapi isinya tidak seperti top di 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
amfibi
sumber

Jawaban:

15

Masalahnya adalah bahwa topsecara default termasuk kode pelarian ANSI / VT100. Dalam hal ini, sepertinya yang Anda inginkan hanyalah output teks "mentah" (mis. Bukan informasi bahwa beberapa teks harus dicetak tebal, dll.). Untungnya, topmemiliki dukungan bawaan untuk mode operasi ini, yang disebut "mode batch" dalam dokumentasi mereka.

Dari halaman manual teratas ( 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.

Begitu...

top -n 1 -b > top.out

Untuk utilitas command-line lainnya yang tidak memiliki saklar tipe "-b" yang nyaman seperti atasan, mungkin juga dilakukan dengan ansifilter . Unduh, kompilasi, dan instal. Maka kamu bisa ...

top -n 1 | ansifilter > top.out
Christopher
sumber