Skip to content

Instantly share code, notes, and snippets.

@weshouman
Last active October 28, 2017 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weshouman/79ea6d562d5532912dfb to your computer and use it in GitHub Desktop.
Save weshouman/79ea6d562d5532912dfb to your computer and use it in GitHub Desktop.
my display sorted processes script #bash
#!/bin/bash
# ---
# @author newlido
# @version 0.1
#
# HOWTO:
# ./myps.sh
# No options yet
#
# Notes:
# sorting for the second time happens only for the highest 10 .. to make the preview better (highest show up first)
# for text colouring check: http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
# for switch cases check: http://www.thegeekstuff.com/2010/07/bash-case-statement/
#
# TODO:
# [x] play with colors
# [] pass a single value to a single ps command (instead of multiple ones)
# [] accept user input of tailing processes
#
# -------------------------------------------------------
echo -n "Sort first 10 processes according to [mem or cpu or return]?: "
read memcpu
case $memcpu in
[m] | [mM][Ee][mM] )
echo -e "\e[1;31mSorting according to mem\e[0m";
ps ax --no-headings -o user,pid,%cpu,%mem,vsz,sgi_rss,tname,stat,start_time,time,ucmd |sort -nk 3|tail -10 | sort -rnk 3;
;;
[cC] | [c|C][P|p][U|u] )
echo "$(tput setaf 1)Sorting according to CPU$(tput sgr0)";
ps ax --no-headings -o user,pid,%cpu,%mem,vsz,sgi_rss,tname,stat,start_time,time,ucmd |sort -nk 4|tail -10 | sort -rnk 4;
;;
[rR] | [r|R][e|E][T|t][U|u][R|r][N|n] )
red='\e[0;31m';
NC='\e[0m' # No Color;
echo -e "${red}Return${NC}";
exit 1
;;
*) echo "Invalid input"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment