memtop: order top by %Mem using expect
Top is one of the commands I use the most while working on linux servers. It’s terribly useful and configurable, got a clean interface and is simple to modify its output using shortcuts.
A lot of times I need to order top by memory percentage instead of cpu percentage. This can be easily achieved by pressing ‘M’ while looking at top.
As I’m really (terribly) lazy and I wanted to test my ‘expect’ discovery, I decided to create a simple script (can be also used as an alias I believe) to run top ordered by %mem directly from bash.
Please note that I know about existing python/perl script that creates mem orderer output from ps command, but that’s not what I wanted…
This is one of my firsts expect scripts, more will come as I really love it… here’s what came out
#!/bin/bash
# This script depends on expect tcl utility.
# Please install it before running this code.
expect -c "
spawn top
expect order
send \"M\"
expect eof
"