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

Bowen, Tricia tbowen at CapitalThinking.com
Fri Sep 19 16:39:10 EST 2003


You can add the following alias to your .bashrc file:

    alias pidof "ps | awk '{print \$1}'"

awk will get the first ($1) column from the list of processes. Use $2 if PID is the second column.

or

>    kill -s testsig# $ ps | grep test | awk '{print $1}' | xargs kill
--Tricia

-----Original Message-----
From: Michelle Murrain [mailto:tech at murrain.net]
Sent: Friday, September 19, 2003 4:16 PM
To: ruchika khera
Cc: techtalk at linuxchix.org
Subject: Re: [Techtalk] how to get PID without using pidof command?


On Fri, 2003-09-19 at 15:09, ruchika khera wrote:
>    Hi,
> 
>    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. It does support other popular
>    commands such as ps, grep, kill, ls.
> 
>    I have a shell script that gets a process's PID and sends it a signal.
>    kill -s testsig# $(pidof test)
> 
>    Can someone help me do this without using pidof?

I'm horrible at shell scripting, but what I'd do in perl is:

#!/usr/bin/perl
use strict;

my $process = `ps aux | grep test`;  ## where 'test' is the name of the
process;
(my $owner, my $pid, my $junk) = split(' ',$process); # get the pid from
that process listing
my $killed = system ("kill -s HUP $pid"); #kill the process


You might want to parse the exit status of the kill command, which will
be in $killed.

I'm sure that a shell scripter (as well as a better perl coder) could
come up with a much more elegant solution...

HTH
-- 
.Michelle
---------------------------
Michelle Murrain
tech at murrain.net
http://www.murrain.net
ph:413-253-2874
cell:413-222-6350
AIM:pearlbear0 Y!:pearlbear9 ICQ:129250575

"Windows is not ready for the Internet" - comment on security blog

_______________________________________________
Techtalk mailing list
Techtalk at linuxchix.org
http://mailman.linuxchix.org/mailman/listinfo/techtalk


More information about the Techtalk mailing list