Monitor the performance of any command line program with this single command
Tags: tips, linux, macos, productivity
Published: 2018-07-07 08:32:54

---
title: Monitor the performance of any command line program with this single command
published: true
description: How to use top/htop utilities in a single command to actively monitor the resource usage any command line program on Linux and Mac
canonical_url: https://agrimprasad.com/post/monitor-the-performance-of-any-command-line-program-with-this-single-command/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/5qnd6x2ndxvd0s0xxqsv.png
tags: tips,linux,macos,productivity
---

This post was originally published on my blog, find original post [here](https://agrimprasad.com/post/monitor-the-performance-of-any-command-line-program-with-this-single-command/).

I have been executing a lot of bash scripts and one-off processes on the linux command line recently, and was curious about how to monitor the CPU and memory usage of these processes as quickly as possible. Some of these processes seemed to bring my linux box to a screeching halt, and without instant resource monitoring I couldn't tell whether such a process was eating excessive RAM or CPU (or both?!)

## What people usually do

Conventionally, [suggestions about process monitoring](https://unix.stackexchange.com/questions/554/how-to-monitor-cpu-memory-usage-of-a-single-process) involve the following:

1. Run your command line process.

1. Get the PID of the process with [`ps aux`](https://unix.stackexchange.com/questions/106847/what-does-aux-mean-in-ps-aux) command which is generally available for both Linux and Mac.

1. Run the utility [`top`](http://man7.org/linux/man-pages/man1/top.1.html) with 

        top -p <pid>

    on Linux or 

        top -pid <pid>

    on Mac OS.

However, I wanted to run commands which would exit within a few seconds, before I could run `top` to start monitoring them. How could I start monitoring these commands as soon as they were invoked?

## What can we do better

As always, Unix has an answer:

1. Run your command in the background and pipe the output to `/dev/null` so that it doesn't get printed on the command line while you are monitoring the CPU/memory usage, e.g. 

        unzip ~/Downloads/sample.zip > /dev/null &

1. Echo the PID of this background process with 

        echo $!

1. Combine the two, and pass the pid of the process to `top` with 

        top -p `{ unzip ~/Downloads/sample.zip > /dev/null & } && echo $!`

    on linux or

        top -pid `{ unzip ~/Downloads/sample.zip > /dev/null & } && echo $!`

    on Mac OS.

BOOM, you just started monitoring your process immediately with a single command. Now isn't that peachy!

## Bonus: Use `htop` instead of `top`

I ❤️ [htop](https://github.com/hishamhm/htop) which is a process monitoring utility available for both linux and Mac OS. Come on, you've got colours! Let's use this instead of `top` for better insight into the CPU/memory performance of our processes.

1. Install `htop` with 

        sudo apt-get install htop

    on Ubuntu or 

        brew install htop

    on Mac OS with [Homebrew](https://brew.sh/)

1. Run our previous monitoring command using `htop` instead of `top` for monitoring with 

        htop -p `{ unzip ~/Downloads/sample.zip > /dev/null & } && echo $!`


See the single command in action below!
![Single command process monitoring with htop](https://i.imgur.com/FepzxVp.gif)


## Double Bonus: Style your editor for a more beautiful htop

Saw some chatter on Twitter about this post which mentioned that I should have added information about the terminal theme I'm using here, since htop gets styled by the terminal and theme in use. I totally agree, so here's my setup:

1. Terminal emulator: [iTerm2](https://www.iterm2.com/) Awesome Mac OS Terminal replacement with cool features such as grid tabs and more.
1. Unix shell + theming framework: `zsh` + [oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh) Check out [this awesome post](https://dev.to/lauragift21/quick-setup-of-oh-my-zsh-terminal-on-ubuntu-2c4e) about how to install these (similar steps on Ubuntu or Mac OS)
1. `oh-my-zsh` theme: Default theme `robbyrussell`. After setting up `oh-my-zsh`, you can specify the desired theme in the `~/.zshrc` file, e.g. `ZSH_THEME="robbyrussell"`