Re: service; ps & grep help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 25Aug2008 09:02, James Pifer <jep@xxxxxxxxxxxxxxxx> wrote:
| I could use a little help with ps and grep. When running a command like:
| # ps -ewf | grep sendmail
| root      2730     1  0 Jul14 ?        00:00:01 sendmail: accepting connections
| root      6500  6362  0 07:51 pts/3    00:00:00 grep sendmail
| 
| Is there any way to run this command and get these results, but exclude
| the actual grep itself, which is the last line?

The traditional hack is this:

  ps -ewf | grep 'send[m]ail'

which will match the sendmail but not match the grep.
But it _is_ a hack. There are better ways.

| A little background, I have a java based application that I've used a
| custom start and stop script for. Basically the stop script does:
| stop() {
|         for pid in `ps -efww | grep myapp | grep -v grep | cut -b 10-15`;do
|                 #echo $pid
|                 kill -9 $pid
|         done
|     RETVAL=$?
|     return $RETVAL

Why not "return $?" or even just "return" - it returns with the last
exit status anyway...

| This has worked for years, but for some reason it has stopped working. I
| think it may be because the process is killing itself before it kills
| the app?

That's possible.

| I assume the correct way to do this is store the pid in a file that you
| reference, but I haven't figured out how to do that yet. 

Like this:

  start-the-java-app &
  echo $! >/var/run/the-pid-file.pid

Later:

  kill `cat /var/run/the-pid-file.pid`  # do you _really_ need -9 ?

If you want less reliable methods (ps|grep), I recommend something like:

  ps ax -o 'pid command' | awk '$2 == "your-java-app" { print $1 }'

but ps|grep is inherently unreliable (vague and racey).

I have personally had to visit a data centre because a (old) RedHat
ssh upgrade did a "killall sshd", which is essentially ps|grep, and
because we had a special purpose sshd, it ate that too despite having
no remit for it.  All because they _didn't_ rely on a pid file., which
is essentially ps|grep, and because we had a special purpose sshd, it
ate that too despite having no remit for it.  All because they _didn't_
rely on a pid file.

Going from reliable (trust the pid file) to the more cautious (trust the
pid file, but check the pid has not been recycled (daemon dies without
tiying up the pid file, new unrelated process gets the same pid)) you
can do something like this:

  if [ -s /var/run/the-pid-file.pid ]
  then
    pid=`cat /var/run/the-pid-file.pid`
    if [ -n "$pid" ]
    then
      pidcmd=`ps -p "$pid" -o command | awk '{print $1}'`
      if [ "x$pidcmd" = 'xyour-java-command' ]
      then
        kill "$pid"
      fi
    fi
  fi

i.e. check that the pid is still associated with the original daemon.

If you want to be safer (i.e. not root) you can use "su" to start the
daemon and kill it as a special purpose user - that way the daemon can't
accidentally do stuff it shouldn't, and your kill can't accidentally
kill something it shouldn't.

Cheers,
-- 
Cameron Simpson <cs@xxxxxxxxxx> DoD#743
http://www.cskk.ezoshosting.com/cs/

I agree with you 100%, it IS personal taste. What I am saying is you
have bad taste. - Gabe _Penny_Arcade_ 23mar2003

-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
Guidelines: http://fedoraproject.org/wiki/Communicate/MailingListGuidelines

[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux