»
S
I
D
E
B
A
R
«
Is that even running?
February 20th, 2009 by masukomi

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 &#8220;got $APP&#8221;`<br />
if [ "$RESULTS" != ""  ]; then<br />
echo $RESULTS<br />
else<br />
echo &#8220;No $APP found with ps -A S&#8221;<br />
fi</p>
<p>

If you can improve on this, please let me know.


5 Responses  
  • Andy Mortimer writes:
    February 20th, 2009 at 3:36 pm

    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.

  • Jakub Nar?bski writes:
    February 20th, 2009 at 7:19 pm

    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.

  • Aristotle Pagaltzis writes:
    February 20th, 2009 at 8:35 pm

    Jakub’s suggestions are good; personally I prefer pgrep, as in “pgrep tomcat”.

  • Aristotle Pagaltzis writes:
    February 20th, 2009 at 8:36 pm

    (And then of course “alias got=pgrep”. ;-) )

  • Jakub Narebski writes:
    February 20th, 2009 at 9:25 pm

    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”;


Leave a Reply

»  Substance: WordPress   »  Style: Ahren Ahimsa
© Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.