Watch your processes in terminal

Published by bolmaster2

I recently learned about the watch command. It doesn’t come with macos as a default. You need to manually install it. Use homebrew:

brew install watch

I was happy. As always, that didn’t last long: I ran into troubles. I was frustrated because I couldn’t pipe grep together with the watch command like this:

watch ps | grep node

It doesn’t work. I found: https://superuser.com/questions/140461/using-watch-with-pipes quickly though. Phew.

I learned about the quotes:

watch 'ps | grep node'

I use to monitor node processes like this:

watch -n 1 'ps -eo comm,rss,%mem,%cpu | grep node'

To show the KB in MB instead:

watch -d -n 1 $'ps -o rss,comm | grep node | awk \'{print $1}\' | xargs -I {} echo \'scale=2; {} / 1024\' | bc'

Doing the calculation with awk doesn’t require that much piping:

watch -d -n 1 $'ps -o rss,comm | grep node | awk \'{print $1/1024}\''

If you want to keep history a while loop would be suitable:

while true; do ps -o rss,comm | grep node | awk '{print $1/1024}'; sleep 1; done