File: nps.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2020-2025 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the “Software”), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # nps [seconds...] [options...] [ps options...] 27 # 28 # 29 # Nice `Process Show` displays all current processes using ANSI styles, so 30 # things are easier to scan/read. All data come from the `ps` command: when 31 # not given any `ps`-specific options, `ps aux` is run by default. 32 # 33 # The options are the same as those for `ps`, as well as the ones listed 34 # below, available in single and double-dashed versions 35 # 36 # -c reverse-sort entries by latest sampled CPU use 37 # -cpu reverse-sort entries by latest sampled CPU use 38 # 39 # -h show this help message 40 # -help show this help message 41 # 42 # -i sort entries by PID 43 # -id sort entries by PID 44 # -pid sort entries by PID 45 # 46 # -m reverse-sort entries by latest sampled RSS (memory) use 47 # -mem reverse-sort entries by latest sampled RSS (memory) use 48 # -rss reverse-sort entries by latest sampled RSS (memory) use 49 50 51 case "$1" in 52 -h|--h|-help|--help) 53 awk '/^# +nps /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 54 ps --help 55 exit 0 56 ;; 57 esac 58 59 for arg in "$@"; do 60 case "${arg}" in 61 --) break;; 62 a|u|x|au|ax|ua|ux|xa|xu) options="${options} ${arg}";; 63 aux|axu|uax|uxa|xau|xua) options="${options} ${arg}";; 64 -c|--c|-cpu|--cpu) options="${options} --sort=-%cpu,-rss";; 65 -i|--i|-id|--id|-pid|--pid) options="${options} --sort=pid";; 66 -m|--m|-mem|--mem|-rss|--rss) options="${options} --sort=-rss,-%cpu";; 67 -u|--u|-user|--user) options="${options} --sort=user,-%cpu,-rss";; 68 --sort=*) options="${options} ${arg}";; 69 esac 70 done 71 72 res="$(ps ${options:-aux})" 73 code=$? 74 if [ "${code}" -ne 0 ]; then 75 return "${code}" 76 fi 77 78 echo "${res}" | 79 80 # restyle numbers with at least 4 digits to make them easier to read; restyle 81 # lines where root is the user; make 0 values stand out in any line 82 sed -E \ 83 -e 's-([0-9]{1,3})([0-9]{6}|[0-9]{3})( |$)-\x1b[36m\1\x1b[0m\2\3-g' \ 84 -e '/^root/s-\x1b\[0m-\x1b[0m\x1b[33m-g; /^root/s-^-\x1b[33m-' \ 85 -e 's-0(\.00*|:00)-\x1b[34m0\1\x1b[0m-g' | 86 87 # add a header line with the current time/date and empty lines to chunk things 88 awk ' 89 BEGIN { 90 now = strftime("%a %b %d %Y %H:%M:%S") 91 printf "%26s\x1b[7m%s\x1b[0m\n\n", "", now 92 } 93 (NR - 1) % 5 == 1 { print "" } 94 1 95 ' | 96 97 less -MKiCRS --header=3