Skip to main content
1 of 5
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

  • One time:

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

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

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

b. with headers

  • One time:

    ps auxc | perl -lae "BEGIN {@x=()}; push @x, [map \$_, @F] if \$#x==-1; \$F[5]=sprintf q[%0.3fMB], \$F[5]/2**10; push @x, [map \$_, @F] if \$F[10]=~/python/; END {print map qq[@{ \$_ }\n], @x }" | column -t

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

    watch -n 0.5 'ps auxc | perl -lae "BEGIN {@x=()}; push @x, [map \$_, @F] if \$#x==-1; \$F[5]=sprintf q[%0.3fMB], \$F[5]/2**10; push @x, [map \$_, @F] if \$F[10]=~/python/; END {print map qq[@{ \$_ }\n], @x }" | column -t'

  • make an alias (bash 4.4 or higher)

    alias watch-python=$'watch -n 0.5 \'ps auxc | perl -lae "BEGIN {@x=()}; push @x, [map \$_, @F] if \$#x==-1; \$F[5]=sprintf q[%0.3fMB], \$F[5]/2**10; push @x, [map \$_, @F] if \$F[10]=~/python/; END {print map qq[@{ \$_ }\n], @x }" | 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 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