Watch your processes in terminal

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, but that didn’t last long: I quickly ran into troubles. I got 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.

Using quotes

It will run the whole command inside the quotes every update.

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}\''

Keep history with a while loop

Or if you just don’t want to use watch. A regular while loop is handy:

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