The other answers are very good and basically wrap up your ad hoc solution of ps | grep
into a shell alias or function. There is nothing wrong with that approach. Note, however, that this means ps
will be extracting information for all running processes and then you have awk
or grep
filtering out the lines of interest.
I would propose that it's slightly more efficient to use pgrep
to get a list of PIDs (as you show in your question), and use ps
to get the output format you want for only the PIDs matched by pgrep
.
Simple process match, POSIX-style full listing
pg() {
pids=`pgrep -d, "$1"`
[ "$pids" ] && ps -f -p "$pids"
}
pg myprocess
This shows a full ps
listing for the matched process names. This function definition can be added to ~/.bashrc
to always be defined in your interactive shell. This can also be modified in a few different ways to change the output format or to match full command lines instead of just the process name.
Simple process match, BSD-style user listing
pg() {
pids=`pgrep -d, "$1"`
[ "$pids" ] && ps up "$pids"
}
Full command-line match, BSD-style job control listing
pg() {
pids=`pgrep -f -d, "$1"`
[ "$pids" ] && ps jp "$pids"
}
Full command-line match, POSIX-style long listing
pg() {
pids=`pgrep -f -d, "$1"`
[ "$pids" ] && ps -l -p "$pids"
}
Note the -f
option on pgrep
in the last two examples to match on the full command line. You can alter these examples to suit your needs, the important part being that the p
or -p
option is given with the list of PIDs found by pgrep
.
pgrep myprocess
do you need to get? What Doesn't that do that you need?