[Techtalk] how to get PID without using pidof command?

Kai MacTane kmactane at GothPunk.com
Fri Sep 19 13:43:36 EST 2003


At 9/19/03 01:16 PM , Michelle Murrain wrote:
>On Fri, 2003-09-19 at 15:09, ruchika khera wrote:
> >    Just when I figured out how to use pidof and kill commands to signal a
> >    process, I learnt that the embedded platform I am using does not
> >    support pidof and find commands.

On the platform I use, pidof(1) is a pretty lame utility that just looks to 
see if there's a pidfile in /var/run, so I tend not to use it, either.

>I'm horrible at shell scripting, but what I'd do in perl is:
>[script]
>I'm sure that a shell scripter (as well as a better perl coder) could
>come up with a much more elegant solution...

I don't know if this is "elegant", but it's shell, at least:

#!/bin/sh
kill -s $2 `ps auxw | grep $1 | grep -v grep | awk '{print $2}'`


The slightly spooky thing about this one is, $2 refers to two completely 
different things in the two places where it's used. In the kill command, it 
means "the second argument given to the script" (in this case, the signal 
to send). In the awk command, it mean to print "the second field" from each 
line of input (in this case, the PID).

It would also be nice to have some error checking to make sure there are 
valid arguments, and maybe a default option for the signal.

This does have the advantage of being able to kill multiple processes at 
once, though, just like killall(1).

                                                 --Kai MacTane
----------------------------------------------------------------------
"You wear guilt/Like shackles on your feet;
  Like a halo in reverse..."
                                                 --Depeche Mode,
                                                  "Halo"



More information about the Techtalk mailing list