Question : J'essaie de capturer la sortie de la commande top dans un fichier. Lorsque j'exécute top> output.txt, le fichier output.txt contient beaucoup de caractères inutiles. Quelle est la meilleure méthode pour capturer la sortie de la commande top dans un fichier texte lisible ?
Réponse : Utilisez l'option d'opération en mode batch de la commande supérieure ( -b ) pour capturer la sortie de la commande supérieure dans un fichier.
Si vous essayez de rediriger la sortie de la commande supérieure vers un fichier texte comme indiqué ci-dessous, vous remarquerez que le fichier de sortie contient beaucoup de caractères inutiles.
Lorsque vous essayez d'afficher le fichier de sortie à l'aide de la commande less, vous remarquerez que le fichier de sortie est créé avec beaucoup de caractères inutiles.
$ top -n 1 > top-output.txt $ less top-output.txt "top-output.txt" may be a binary file. See it anyway?
Remarque : L'option -n 1 indique qu'une seule itération de la commande top doit être exécutée.
Pour éviter ce problème et obtenir une sortie de commande top lisible, utilisez l'option -b dans la commande top. Exécutez la commande supérieure en mode batch comme indiqué ci-dessous.
$ 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
À propos des options -b et -n de la page de manuel de la commande supérieure :
-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.
Vous pouvez également utiliser cette méthode pour rediriger la sortie de la commande top vers un autre programme, comme indiqué ci-dessous.
$ 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