Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Fedora User (http://www.linux-archive.org/fedora-user/)
-   -   service; ps & grep help (http://www.linux-archive.org/fedora-user/148461-service-ps-grep-help.html)

Sjoerd Mullender 08-25-2008 01:38 PM

service; ps & grep help
 
Anders Karlsson wrote:
> * James Pifer <jep@obrien-pifer.com> [20080825 15:03]:
>> 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
>> smmsp 2739 1 0 Jul14 ? 00:00:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
>> root 6500 6362 0 07:51 pts/3 00:00:00 grep sendmail
>
> Try
> # ps -ef | grep [s]endmail
> instead. Should do what you want (does for me anyway).

Even easier:

pgrep sendmail

will just print the process ID of sendmail. Nothing else.
Also look at pkill. You'll need to yum install procps if you don't have
it already.

>> Is there any way to run this command and get these results, but exclude
>> the actual grep itself, which is the last line?
>>
>> 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
>> }
>
> Well, in a shell, $$ is the PID. If you can capture your process PID
> when it starts, you simply write it in a file in /var/run/ and when
> you stop, you issue a "kill -9 $(</var/run/pidfile)".
>
>> 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?
>>
>> 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.
>>
>> Any help is appreciated.
>
> You can have a look at various init scripts in /etc/init.d/ to get an
> idea about how they done it.
>
> /Anders
>


--
Sjoerd Mullender

--
fedora-list mailing list
fedora-list@redhat.com
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list

James Pifer 08-25-2008 01:40 PM

service; ps & grep help
 
> Try
> # ps -ef | grep [s]endmail
> instead. Should do what you want (does for me anyway).

Adding the brackets like you've shown seems to do the trick.

> Well, in a shell, $$ is the PID. If you can capture your process PID
> when it starts, you simply write it in a file in /var/run/ and when
> you stop, you issue a "kill -9 $(</var/run/pidfile)".
>

$$ is the pid of the actual shell, not the process I'm trying to find or
kill, unless knowing that pid somehow leads me to the pid I'm looking
for?

Either way, adding the brackets seems to fix my immediate problem.

Thanks,
James

--
fedora-list mailing list
fedora-list@redhat.com
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list

Kevin Martin 08-25-2008 01:49 PM

service; ps & grep help
 
James Pifer 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
smmsp 2739 1 0 Jul14 ? 00:00:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
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?

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
}

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?

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.

Any help is appreciated.


Thanks,
James



Could it be that the script itself is getting caught in your "ps" and is
getting killed (not the grep embedded in the script)? Could it be the
"cut" part of the line (we've always used 'awk '{ print $2 }' ' in our
stop blocks...never tried the cut idea) that's getting messed up somehow?


Kevin

--
fedora-list mailing list
fedora-list@redhat.com
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list

Cameron Simpson 09-06-2008 12:20 AM

service; ps & grep help
 
On 25Aug2008 09:02, James Pifer <jep@obrien-pifer.com> 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@zip.com.au> 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@redhat.com
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
Guidelines: http://fedoraproject.org/wiki/Communicate/MailingListGuidelines

"Patrick O'Callaghan" 09-07-2008 06:59 AM

service; ps & grep help
 
On Sat, 2008-09-06 at 10:20 +1000, Cameron Simpson wrote:
> 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 picky, this is actually not enough. It just tells you
that process $pid is currently running a program with the same name as
the one you want. It may even be the same executable file, but it is not
necessarily the same process as before. Very unlikely, and it may not
matter to you, but it's worth being aware of.

One way round it might be to compare the file creation time
of /var/run/the-pid-file.pid with /proc/<pid>/starttime (some fiddly
arithmetic required here). Clearly the pid file should to be younger
than the process.

poc

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

Bill Davidsen 09-08-2008 12:48 AM

service; ps & grep help
 
James Pifer 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
smmsp 2739 1 0 Jul14 ? 00:00:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
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?

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
}

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?

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.


It is the right way to do this, I can't imagine doing it reliably any
other way.


sleep 10 & echo $! >sleep.pid

Modify to suit your needs. Note: you want to do it the simple way or get
very tricky, bash thinks ! is shell history and does amusing things when
you get just a little tricky. For example:

sleep 10 & echo "Started $!"
doesn't do what you expect.


Any help is appreciated.


Thanks,
James





--
Bill Davidsen <davidsen@tmr.com>
"We have more to fear from the bungling of the incompetent than from
the machinations of the wicked." - from Slashdot

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

Cameron Simpson 09-08-2008 01:09 AM

service; ps & grep help
 
On 07Sep2008 02:29, Patrick O'Callaghan <pocallaghan@gmail.com> wrote:
| On Sat, 2008-09-06 at 10:20 +1000, Cameron Simpson wrote:
| > 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 picky, this is actually not enough. It just tells you
| that process $pid is currently running a program with the same name as
| the one you want. It may even be the same executable file, but it is not
| necessarily the same process as before. Very unlikely, and it may not
| matter to you, but it's worth being aware of.
|
| One way round it might be to compare the file creation time
| of /var/run/the-pid-file.pid with /proc/<pid>/starttime (some fiddly
| arithmetic required here).

Portable only to other Linux systems. Another thing to be aware of.
But there are ps incantations that recite the process start time, so you
could pull it from there if it's precise enough. Point taken.

| Clearly the pid file should to be younger
| than the process.

True. It's a rather coarse check though.

I would much rather keep a caretaker process around. If our concern is
that the daemon dies, badly enough that it doesn't tidy up its pid file
(probable if the daemon isn't responsible for the pidfile in the first
place:-) then I like this:

( start daemon &
echo $! >/var/run/the-daemon.pid
wait
rm /var/run/the-daemon.pid
) &

That way the pid file should get removed if the daemon dies. Unless
something takes out the caretaker subshell (requires special effort, or
a disaster:-) the pid file will be reliably removed.

I have an "ifpid" command for performing the pidfile check augumented
with the "ps|grep" check which accepts an arbitrary regexp here:

http://www.cskk.ezoshosting.com/cs//css/bin/ifpid

This lets one check for command line arguments as well as the command
name. This lets you be a bit more paranoid. I tend to use it like this:

if ifpid -q /var/run/the-pidfile.pid ' command .* -f config-file'
then
do stuff, eg prod daemon to do a log rotate or something
fi

which makes for easy to read scripts.

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

[...] if share-dealing had anything to do with future yields, we'd all
have BT shares and stockbrokers would be wise, Yoda-like figures who
don't touch cocaine for fear of the long-term risks. [...]
- _NTK_, 26jul99 http://www.ntk.net/

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

"Patrick O'Callaghan" 09-08-2008 03:34 PM

service; ps & grep help
 
On Mon, 2008-09-08 at 11:09 +1000, Cameron Simpson wrote:
> I would much rather keep a caretaker process around. If our concern is
> that the daemon dies, badly enough that it doesn't tidy up its pid
> file
> (probable if the daemon isn't responsible for the pidfile in the first
> place:-) then I like this:
>
> ( start daemon &
> echo $! >/var/run/the-daemon.pid
> wait
> rm /var/run/the-daemon.pid
> ) &
>
> That way the pid file should get removed if the daemon dies. Unless
> something takes out the caretaker subshell (requires special effort,
> or a disaster:-) the pid file will be reliably removed.

Yes, that would work. Of course all this would be unnecessary if process
ids were nonces, i.e. never repeated during the lifetime of the system,
but that would be a pretty big change.

poc

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


All times are GMT. The time now is 02:31 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.