Unix command of the day: watch

The project I’m working on right now involve not just Dockerized Rails microservices, Meteor JS, and a data set measured in tens of terabytes, but also a big Bash code base.

Bash is a language that makes it easy to shoot yourself in the foot. I have some thoughts on how to write robust, modular, loosely coupled, unit tested Bash that go beyond Bash strict mode and shellcheck. However, let’s save those for later.

Here’s a useful shell command for today: watch

watch will run a command for you every few seconds and output the results on a clean screen. If you combine it with a split-screen or split-pane tool, you can quickly create a mini-dashboard.

For example, watch -n15 df -h will print your free disk space every 15 seconds:

Every 15.0s: df -h        Tue Apr 14 13:43:25 2015

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             1.8T  691G  1.1T  40% /
tmpfs                 253G   12K  253G   1% /dev/shm
/dev/sda1             248M   58M  178M  25% /boot
/dev/sdb1              30T   12T   18T  41% /my_disk

By way of another example, watch -n60 db2 list utilities show detail will check on the status of your DB2 load and other operations every 60 seconds.

Every 60.0s: db2 list utilities show detail            Mon Apr 13 12:06:24 2015

ID                               = 4
Type                             = LOAD
Database Name                    = MY_DB
Member Number                    = 0
Description                      = [LOADID: 1177.2015-04-13-15.58.12.282010.0 (20;15)] 
[*LOCAL.MY_USER.150413204511] OFFLINE LOAD DEL
AUTOMATIC INDEXING INSERT NON-RECOVERABLE MY_SCHEMA    .MY_TABLE
Start Time                       = 04/13/2015 15:58:12.298788
State                            = Executing
Invocation Type                  = User
Progress Monitoring:
   Phase Number                  = 1
      Description                = SETUP
      Total Work                 = 0 bytes
      Completed Work             = 0 bytes
      Start Time                 = 04/13/2015 15:58:12.298799

   Phase Number [Current]        = 2
      Description                = LOAD
      Total Work                 = 4105362974 rows
      Completed Work             = 1069904365 rows
      Start Time                 = 04/13/2015 15:58:15.821695

   Phase Number                  = 3
      Description                = BUILD
      Total Work                 = 2 indexes
      Completed Work             = 0 indexes
      Start Time                 = Not Started

Mac OS X does not include watch by default. Assuming you have Homebrew, the following will install watch for you:

brew install watch

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.