Skip to main content
4 of 5
added 61 characters in body
stason
  • 111
  • 5

Let's do some magic one liners so that you can alias what you want and never need to remember which letter to hit when in top to get what you want.

The want is to display top like output but having RSS in MBs and I'm adding also a desire to filter out only the program I want.

In the following examples we will use python as the target processes we want to watch. We will, of course, use perl to do that ;)

a. Without headers (if you memorizedps's headers)

  • One time:

    ps auxc | grep python | perl -plae '$F[5]=sprintf q[%0.3fMB],$F[5]/2**10; $_=qq[@F]' | column -t

  • Using watch to emulate top, refreshing every 0.5 sec

    watch -n 0.5 $'ps auxc | grep python | perl -plae \'$F[5]=sprintf q[%0.3fMB],$F[5]/2**10; $_=qq[@F]\' | column -t'

  • Make an alias (bash 4.4 or higher)

    alias watch-python=$'watch -n 0.5 \'ps auxc | grep python | perl -plae "\$F[5]=sprintf q[%0.3fMB],\$F[5]/2**10; \$_=qq[@F]" | column -t | grep python\''

b. With headers (for the rest of us):

  • One time:

    (ps auxc | head -1; ps auxc | grep python | perl -plae '$F[5]=sprintf q[%0.3fMB],$F[5]/2**10; $_=qq[@F]') | column -t

  • Using watch to emulate top, refreshing every 0.5 sec:

    watch -n 0.5 $'(ps auxc | head -1; ps auxc | grep python | perl -plae \'$F[5]=sprintf q[%0.3fMB],$F[5]/2**10; $_=qq[@F]\') | column -t'

  • Make an alias (bash 4.4 or higher)

    alias watch-python=$'watch -n 0.5 \'(ps auxc | head -1; ps auxc | grep python | perl -plae "\$F[5]=sprintf q[%0.3fMB],\$F[5]/2**10; \$_=qq[@F]") | column -t\''

Notes:

  • replace 2**10 with 2**20 and MB with GB if you want GBs instead of MBs
  • replace python with another string that your program starts with. You can inspect the output of ps auxc to see what's the 10th (0-indexed) column says in case you get no output. a python program could be running with the name of the python script and not python itself for example, so make sure to use the name of the script instead.
  • replace %0.3f with %0.2f if you want only 2 decimals for MBs
  • replace watch -n 0.5 with watch -n 3 if you want to refresh every 3 seconds
  • these were tested with bash 5.1.16 - should work with most recent bash versions
  • if you tweak the ps flags in these examples the count of columns may change and the scripts may break.
stason
  • 111
  • 5