GNU/Linux >> Linux Esercitazione >  >> Linux

Come acquisire l'output del comando Unix Top in un file in formato leggibile

Domanda: Sto cercando di acquisire l'output del comando superiore in un file. Quando eseguo top> output.txt, il file output.txt contiene molti caratteri spazzatura. Qual è il metodo migliore per acquisire l'output del comando principale in un file di testo leggibile?

Risposta: Utilizzare l'opzione di funzionamento in modalità batch del comando principale ( -b ) per acquisire l'output del comando principale in un file.

Se provi a reindirizzare l'output del comando in alto a un file di testo come mostrato di seguito, noterai che il file di output contiene molti caratteri spazzatura.

Quando provi a visualizzare il file di output utilizzando less command, noterai che il file di output viene creato con molti caratteri spazzatura.

$ top -n 1 > top-output.txt

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

Nota: L'opzione -n ​​1 indica che deve essere eseguita solo un'iterazione del comando superiore.

Per evitare questo problema e ottenere un output leggibile del comando superiore, utilizzare l'opzione -b nel comando superiore. Esegui il comando top in modalità batch come mostrato di seguito.

$ 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       

Informazioni sull'opzione -be -n dalla pagina man dei comandi in alto:

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

Puoi anche usare questo metodo per reindirizzare l'output del comando top a un altro programma come mostrato di seguito.

$ 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. Come salvare l'output dei comandi in un file in Linux

  2. Visualizza l'output dei comandi o il contenuto del file in formato colonna

  3. Come aggiungere l'output a un file?

  4. Come reindirizzare l'output del comando time su un file in Linux?

  5. Come stampare l'output del comando Linux su un file?

Come reindirizzare l'output del comando della shell

Come convertire un file Windows in un file UNIX

Come leggere l'output e gli usi dei comandi principali di Linux

Come usare il comando TOP

Come visualizzare la dimensione del file in formato leggibile dall'uomo (KB, MB, GB) nel terminale Linux

Tutorial Unix Sed:come scrivere su un file usando Sed