bash ps print info about process with name
You can always use the two-stage approach.
1.) find the wanted PID
s. For this use the simplest possible ps
ps -o pid,comm | grep "$2" | cut -f1 -d' '
the ps -o pid,comm
prints only two columns, like:
67676 -bash71548 -bash71995 -bash72219 man72220 sh72221 sh72225 sh72227 /usr/bin/less74364 -bash
so grepping it is easy (and noise-less, without false triggers). The cut
just extracts the PIDs. E.g. the
ps -o pid,comm | grep bash | cut -f1 -d' '
prints
67676715487199574364
2.) and now you can feed the found PIDs
to the another ps
using the -p
flag, so the full command is:
ps -o uid,pid,ppid,ni,vsz,rss,stat,tty,time,command -p $(ps -o pid,comm | grep bash | cut -f1 -d' ')
output
UID PID PPID NI VSZ RSS STAT TTY TIME COMMAND 501 67676 67675 0 2499876 7212 S+ ttys000 0:00.04 -bash 501 71548 71547 0 2500900 8080 S ttys001 0:01.81 -bash 501 71995 71994 0 2457892 3616 S ttys002 0:00.04 -bash 501 74364 74363 0 2466084 7176 S+ ttys003 0:00.06 -bash
e.g. the solution using the $2
is
ps -o uid,pid,ppid,ni,vsz,rss,stat,tty,time,command -p $(ps -o pid,comm | grep "$2" | cut -f1 -d' ')