File: processes.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 # processes [options...] 27 # 28 # Show all current processes as a table of tab-separated values (TSV), whose 29 # first line has the column names. 30 # 31 # All data come from running the `ps` command: when not given any options, 32 # the default is to run `ps aux --sort=pid`. 33 # 34 # Besides the usual `ps`-specific options, this script offers easier-to-use 35 # options 36 # 37 # --c reverse-sort entries by latest sampled CPU use 38 # --cpu reverse-sort entries by latest sampled CPU use 39 # 40 # --h show this help message 41 # --help show this help message 42 # 43 # --m reverse-sort entries by latest sampled RSS (memory) use 44 # --mem reverse-sort entries by latest sampled RSS (memory) use 45 # --rss reverse-sort entries by latest sampled RSS (memory) use 46 # 47 # --u sort/group entries by user 48 # --user sort/group entries by user 49 50 51 52 case "$1" in 53 -h|--h|-help|--help) 54 awk '/^# +processes /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 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) select="${select}${arg}";; 63 # aux|axu|uax|uxa|xau|xua) select="${select}${arg}";; 64 --c|--cpu) sort="--sort=-%cpu,-rss";; 65 --i|--id|--p|--pid) sort="--sort=pid";; 66 --m|--mem|--rss) sort="--sort=-rss,-%cpu";; 67 --u|--user) sort="--sort=user,-%cpu,-rss";; 68 # --u|--user) sort="--sort=user,-rss,-%cpu";; 69 --sort=*) sort="${arg}";; 70 *) select="${select}${arg}";; 71 esac 72 done 73 74 res="$(ps ${select:-aux} ${sort:---sort=pid})" 75 76 echo "${res}" | awk ' 77 NR == 1 { cut = match($0, /(CMD|COMMAND)$/) ? RSTART : 0 } 78 cut == 0 { print; next } 79 { 80 lead = substr($0, 1, cut - 1) 81 gsub(/^ +| +$/, "", lead) 82 gsub(/ +/, "\t", lead) 83 printf "%s\t%s\n", lead, substr($0, cut) 84 } 85 '