It’s not uncommon for me to wonder if some app is running on my linux box, and while I could pipe together ps and a couple greps it felt silly to keep doing it after a while. So, I applied my admittedly limited bash skills and came up with the following script which I throw that in an executable called “got”. Now I can just type “got tomcat?” (the question-mark is optional). If anything is running with “tomcat” in it’s command it’ll give me the skinny on it. Otherwise it’ll let me know it wasn’t found.
</p> <p>#!/bin/bash<br /> APP=`echo $1 | sed s/?$//`<br /> RESULTS=`ps -A S| grep $APP | grep -v grep | grep -v “got $APP”`<br /> if [ "$RESULTS" != "" ]; then<br /> echo $RESULTS<br /> else<br /> echo “No $APP found with ps -A S”<br /> fi</p> <p>
If you can improve on this, please let me know.
There’s a neat trick I stumbled on somewhere which avoids the need for the second grep, and makes the raw commands a lot more pleasant to type. It takes advantage of a character class containing only one letter, but which breaks up the string so it doesn’t match itself:
ps -eaf | grep ‘[t]omcat’
(Use your favourite switches in place of -eaf). Because “[t]omcat” doesn’t contain the string “tomcat”, the grep command itself doesn’t match.
If you have it installed, you can use `pidof` (on my system at /sbin/pidof) to select process(es) by pid(s), instead of using pipeline of grep to filter output of ps. Or you can use ‘-C cmdlist’ option of ps.
Jakub’s suggestions are good; personally I prefer pgrep, as in “pgrep tomcat”.
pgrep
pgrep tomcat
(And then of course “alias got=pgrep”. ;-) )
alias got=pgrep
pidof is part of Sys V init scripts;
pgrep is from procps (which you have probably installed for ‘top’ utility);
you have to match command name exactly (e.g. emacs-x for GNU Emacs in X Window) for “ps -C cmdlist”;
Name (required)
Mail (will not be published) (required)
Website