Pregunta: Estoy tratando de capturar la salida del comando superior en un archivo. Cuando ejecuto top> output.txt, el archivo output.txt contiene muchos caracteres basura. ¿Cuál es el mejor método para capturar la salida del comando superior en un archivo de texto legible?
Respuesta: Use la opción de operación en modo por lotes del comando superior ( -b ) para capturar la salida del comando superior en un archivo.
Si intenta redirigir la salida del comando superior a un archivo de texto como se muestra a continuación, notará que el archivo de salida contiene muchos caracteres basura.
Cuando intente ver el archivo de salida usando menos comando, notará que el archivo de salida se crea con muchos caracteres basura.
$ top -n 1 > top-output.txt $ less top-output.txt "top-output.txt" may be a binary file. See it anyway?
Para evitar este problema y obtener una salida de comando superior legible, use la opción -b en el comando superior. Ejecute el comando superior en modo por lotes como se muestra a continuación.
$ 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
Acerca de la opción -b y -n de la página del comando man superior:
-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.
También puede usar este método para redirigir la salida del comando superior a otro programa como se muestra a continuación.
$ 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