1

I'm running a resource intensive python application but would like to know the CPU usage of this application constantly every 5 seconds and dispay the results.

I use mpstat -P ALL but I need to rerun this command every 5 seconds.

Is there a way to poll and display the results constantly every 5 seconds. I would like to format the output as well.

dessert
  • 39,982

1 Answers1

1

As others already mentioned, watch is the way to go to watch how a command's output changes. However if you want to modify the output and/or use it e.g. in a script, I'd use a loop and printf:

while :; do
  printf "%s %.1f %s\r" "I need" "$(mpstat -P ALL | awk 'NR==4{print $3}')" "here."
  sleep 5
done

This will print "I need X here.", wait 5 seconds and repeat overwriting the existing line of text.
X in this example is the value in row 4, column 3 of mpstat's output, formatted by as a float rounded to 1 decimal. Press Ctrl+C to break the loop.

dessert
  • 39,982