> $LOG_FILE sleep 2 done Usage : ./cpu_usage_ps.sh [commande à analyser]..."> Script bash pour loguer l'usage cpu d'une commande
Script ultra basique pour loguer l'usage cpu d'une commande linux
#!/bin/bash
PNAME="$1"
LOG_FILE="$2"
while true ; do
ps aux | grep -i $PNAME | grep -v grep | awk '{print $3}' >> $LOG_FILE
sleep 2
done
Usage : ./cpu_usage_ps.sh [commande à analyser] [chemin_fichier.log]
Explications :
ps aux : affiche tous les process, exemple :
mike@penguin:/mnt/chromeos/MyFiles/Downloads/alice/CPU_LOG$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 100220 10412 ? Ss 15:27 0:01 /sbin/init
root 65 0.0 0.6 86816 43392 ? Ss 15:27 0:02 /lib/systemd/systemd-journald
root 73 0.0 0.0 21148 5976 ? Ss 15:27 0:00 /lib/systemd/systemd-udevd
root 91 0.0 0.0 8184 4712 ? Ss 15:27 0:01 /usr/sbin/haveged --Foreground --verbose=1
root 96 0.0 0.1 236364 7232 ? Ssl 15:27 0:00 /usr/libexec/accounts-daemon
avahi 99 0.0 0.0 7332 3660 ? Ss 15:27 0:03 avahi-daemon: running [penguin.local]
root 100 0.0 0.0 6748 2760 ? Ss 15:27 0:00 /usr/sbin/cron -f etc.
grep -i [process] : affiche uniquement les lignes contenant le nom du process, ex:
mike@penguin:/mnt/chromeos/MyFiles/Downloads/alice/CPU_LOG$ ps aux | grep -i gtkalice
mike 4044 0.0 1.2 1114276 86508 pts/1 Sl+ 17:15 0:06 ./gtkaliceX11.x Batman_Rises_disk1.adf Batman_Rises_disk2.adf
mike 4060 0.0 0.0 6244 648 pts/2 S+ 17:15 0:00 grep -i gtkalice
grep -v grep : enlève la ligne 'grep' (oui c'est con mais c'est aussi un process)
mike@penguin:/mnt/chromeos/MyFiles/Downloads/alice/CPU_LOG$ ps aux | grep -i gtkalice | grep -v grep
mike 4044 49.4 1.2 1114276 86852 pts/1 Sl+ 17:15 0:29 ./gtkaliceX11.x Batman_Rises_disk1.adf Batman_Rises_disk2.adf
awk '{print $3}' : affiche uniquement la 3ème colonne (le CPU dans notre cas)
mike@penguin:/mnt/chromeos/MyFiles/Downloads/alice/CPU_LOG$ ps aux | grep -i gtkalice | grep -v grep | awk '{print $3}'
44.3
>> $LOG_FILE : enregistre le résultat de la commande dans un fichier, plus précisément à la suite de celui-ci (sans l'écraser)
sleep 2 : attendre 2 secondes dans la boucle infinie (while ... done)
/image%2F1460668%2F20250109%2Fob_68458b_logo-mike-3.png)
/image%2F1460668%2F20230306%2Fob_f219e7_screenshot-2023-03-06-07-11-44.png)