2021-10-11 15:03:24 -07:00
|
|
|
#!/usr/bin/env sh
|
2024-07-22 05:28:58 -07:00
|
|
|
# compat: +ash +bash +dash +zsh YES_BB_AWK
|
2021-10-11 15:03:24 -07:00
|
|
|
|
|
|
|
# probably not compatible with busybox ps because its column 2 isn't PID.
|
|
|
|
|
|
|
|
running() { ### @- WIP
|
2022-03-06 13:12:17 -08:00
|
|
|
local cmd=0 pid=0 col=
|
2021-10-11 15:03:24 -07:00
|
|
|
local usage='usage: running {cmd|pid} ...'
|
|
|
|
[ $# -gt 0 ] || { printf '%s\n' "$usage" >&2; return 2; }
|
|
|
|
for col; do
|
|
|
|
case "$col" in
|
|
|
|
(cmd) cmd=1;;
|
|
|
|
(pid) pid=1;;
|
|
|
|
(*) printf '%s\n' "$usage" >&2; return 2;;
|
|
|
|
esac
|
|
|
|
done
|
2023-06-15 18:30:43 -07:00
|
|
|
|
|
|
|
# ps -o pid,comm=CMD
|
|
|
|
# argumentless flags accepted by busybox:
|
|
|
|
# a d e f l A T Z
|
|
|
|
# argumentless flags accepted by coreutils:
|
|
|
|
# a c d e f g h j l m n r s t u v w x A F H L M N P S T V X Z
|
|
|
|
|
2021-10-11 15:03:24 -07:00
|
|
|
ps -f | awk -v cmd=$cmd -v pid=$pid '
|
|
|
|
NR==1{
|
|
|
|
s=index($0,"COMMAND")
|
|
|
|
s=s?s:index($0,"CMD")
|
|
|
|
}
|
|
|
|
NR>1{
|
|
|
|
l=substr($0,s)
|
|
|
|
sub(" .*","",l)
|
|
|
|
sub(".*[/\\\\]","",l)
|
|
|
|
if(pid)printf "%s", $2
|
|
|
|
if(cmd&&pid)printf "\t"
|
|
|
|
if(cmd)printf "%s", l
|
|
|
|
printf "\n"
|
|
|
|
}
|
|
|
|
'
|
|
|
|
}
|
|
|
|
|
|
|
|
[ -n "${preload+-}" ] || running "$@"
|