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

Re: how to get pid when I start the process



Zhang Weiwu wrote:
> zhangweiwu@mesopotamia:~$ ps ax | grep mule
> 13595 zhangwei 14080 R N  amuled
> 13598 zhangwei 14080 S N  amuled
> 13600 zhangwei 14080 S N  amuled
> 13603 zhangwei 14080 S N  amuled
> 13612 zhangwei  1352 S    grep mule
> zhangweiwu@mesopotamia:~$ kill 13595
> 
> Note that I should always kill the least-numbered process, I tried
> otherwise and always end up in trouble.

You might try using the ps show process hierchy options to show the
parent child relationship of processes.  You are wanting to kill the
parent process.

SysV-like (my personal preference):

  ps -efH

BSD-like

  ps axf

But do you need to see all processes?  Won't you be the owner of that
process?

  ps xf

> Question is, how do I script-lize this? The only difficulty is to get
> the pid in script. I could use an one-liner awk script to analyses the
> output of ps, but that sounds overkill. Is there a better way?

The best answer is to have the deamon write a pid file itself.  I am
not familiar with amuled and did not install it to investigate but
if it could write a pidfile then you could kill the right pid without
mistake.  I assume not though or I assume you would already be using it.

You could use 'killall' to kill all processes named amuled.  I would
guess that this is your best answer.  Searching the web briefly tells
me that the --ignore-case option may be needed here.

  killall --ignore-case amuled

Alternatively 'pkill' also has a rich set of option controls for
killing processes by name.

If amuled has an option *not* to fork into the background then you
could use that option and then fork into the background yourself after
writing a pidfile.  This is sometimes used by /etc/init.d scripts when
working with daemons with similar issues.  The Debian rsync init.d
script uses this technique for one example.

Bob

Attachment: signature.asc
Description: Digital signature


Reply to: