#!/bin/sh # The MIT License (MIT) # # Copyright © 2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # ctop [seconds...] [options...] # # # Clean TOP runs `top` using better default settings, and offers easier-to-use # command-line options. Updates happen every 2 seconds by default, unless you # change it by giving an argument which looks like a number, with or without # decimals. # # The name comes from the fact that regular `top` shows some output when quit, # while this wrapper tool doesn't. # # As when running `top` directly, pressing `q` quits, while pressing `h` shows # the help screen, which you also quit by pressing `q` while in it. You can # also force-quit this tool by pressing Ctrl+C. # # The options are, available both in single and double-dash versions # # -cpu reverse-sort entries by latest sampled CPU use # # -h show this help message # -help show this help message # # -id sort entries by PID # -pid sort entries by PID # # -m reverse-sort entries by latest sampled RSS/RES (memory) use # -mem reverse-sort entries by latest sampled RSS/RES (memory) use # -res reverse-sort entries by latest sampled RSS/RES (memory) use # -rss reverse-sort entries by latest sampled RSS/RES (memory) use # # -sh reverse-sort entries by latest sampled shared memory use # -shr reverse-sort entries by latest sampled shared memory use # # -time reverse-sort entries by running time # # -usr sort entries by user # -user sort entries by user # # -vir reverse-sort entries by latest sampled VIRTual memory use # -virt reverse-sort entries by latest sampled VIRTual memory use case "$1" in -h|--h|-help|--help) awk '/^# +ctop /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac cmd='top' sort='' seconds=2 number_regex='^(\.[0-9]+|[1-9]+(\.[0-9]+)?)$' for arg in "$@"; do if [ "${arg}" = '--' ]; then break fi case "${arg}" in -cpu|--cpu) sort="-o %CPU";; -id|--id|-pid|--pid) sort="-o PID";; -m|--m|-mem|--mem|-res|--res|-rss|--rss) sort="-o RES";; -sh|--sh|-shr|--shr) sort="-o SHR";; -t|--t|-time|--time|-time+|--time+) sort="-o TIME+";; -usr|--usr|-user|--user) sort="-o USER";; -vir|--vir|-virt|--virt) sort="-o VIRT";; *) if echo "${arg}" | grep -E -q "${number_regex}"; then seconds="${arg}" else cmd="${cmd} ${arg}" fi ;; esac done # restore previous screen contents when sent an INT signal, which usually # happens by pressing Ctrl+C trap 'tput rmcup; exit 130' INT tput smcup ${cmd} -d "${seconds}" ${sort} code=$? tput rmcup exit "${code}"