Unix Process name to PID

Unix Process name to PID

am 09.11.2004 15:36:10 von jcoll253

Is there a single line command to kill a process by process name?

I can get the PID by

ps ax | grep "QM Networker"

But it returns all the process details

"4441 std R+ 0:00.0......."

And passing this output confuses the kill command. So is there something
else I can do to only send data from the output of 'ps ax | grep "QM
Networker"' up to the first (but not including) space to 'kill'?



This is of course if I am on the right track!


Thanks, James.

Re: Unix Process name to PID

am 09.11.2004 15:48:16 von Eric Moors

James Collins wrote:

> Is there a single line command to kill a process by process name?

killall "QM Networker"

> I can get the PID by
>
> ps ax | grep "QM Networker"
>

pidof "QM Networker"

I am running linux, and have no idea whether this is general
unix or linux specific.

Eric

Re: Unix Process name to PID

am 09.11.2004 15:49:30 von Konrad Rzadzinski

Dnia Wed, 10 Nov 2004 00:36:10 +1000, James Collins
tako rzek³:
: Is there a single line command to kill a process by process name?
:
: I can get the PID by
:
: ps ax | grep "QM Networker"
:
: But it returns all the process details
:
: "4441 std R+ 0:00.0......."
:
: And passing this output confuses the kill command. So is there something
: else I can do to only send data from the output of 'ps ax | grep "QM
: Networker"' up to the first (but not including) space to 'kill'?

For example, you could use
# kill `ps ax | grep "your process" | awk '{print $1}'`
but it is, as you see, quite ugly.

--
Konrad
http://amba.matrix.pl/~kurak/
'W ¿yciu nie licz± siê tylko cia³o i musku³y - wa¿ny jest jeszcze fryz.' -
Johnny Bravo

Re: Unix Process name to PID

am 09.11.2004 16:18:58 von cwh0803

On Wed, 10 Nov 2004, James Collins wrote:

> Is there a single line command to kill a process by process name?

Isn't this exactly what pkill is for?

$> pkill -9 "QM Networker"

Hope I'm not too far off base..

Carl

- --

"There are 10 types of people in the world: Those who understand binary
and those that don't."

Re: Unix Process name to PID

am 09.11.2004 17:12:55 von gazelle

In article ,
Carl Holtje ;021;vcsg6; wrote:
>On Wed, 10 Nov 2004, James Collins wrote:
>
>> Is there a single line command to kill a process by process name?
>
>Isn't this exactly what pkill is for?
>
>$> pkill -9 "QM Networker"
>
>Hope I'm not too far off base..

Doing this in classical Unix using only shell tools is:

1) Ugly
2) Error-prone
3) A wheel that is frequenly re-invented

This is why most modern Unixes (e.g., Linux, Solaris) have special tools
designed for the job. Unfortunately, the portability police will tell us
that these tools aren't portable (which is true).

Re: Unix Process name to PID

am 09.11.2004 18:31:52 von Fahd

I have this useful function in my .bashrc.

pskill()
{
if [ -z $1 ]; then
echo -e "\e[0;31;1mUsage: pskill [processName]\e[m";
else
ps -au $USER | grep -i $1 |awk '{print "kill -9 " $1}'|sh
fi
}

--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking... "

Re: Unix Process name to PID

am 10.11.2004 16:36:29 von jselby

It is present in Unix as well, but it doesn't do the same thing. In
Unix it will kill all processes owned by the user calling it.

Here is the man page description:
killall - Terminates all processes started by the user, except the
calling
process

This difference has bitten me before.

Jason

Re: Unix Process name to PID

am 10.11.2004 20:43:24 von Kevin Rodgers

Fahd wrote:
> pskill()
> {
> if [ -z $1 ]; then
> echo -e "\e[0;31;1mUsage: pskill [processName]\e[m";
> else
> ps -au $USER | grep -i $1 |awk '{print "kill -9 " $1}'|sh
> fi
> }

Match the process name against the CMD column only (not the PID, TTY,
and TIME columns). Don't invoke both grep and awk. Use xargs instead
of sh.

cmd=$1
ps -au $USER | awk "\$4 ~ /$cmd/ {print \$1}" | xargs kill -9

--
Kevin Rodgers

Re: Unix Process name to PID

am 11.11.2004 16:29:40 von Dan Mercer

"Kevin Rodgers" wrote in message news:2vf9b0F2kpjjjU1@uni-berlin.de...
: Fahd wrote:
: > pskill()
: > {
: > if [ -z $1 ]; then
: > echo -e "\e[0;31;1mUsage: pskill [processName]\e[m";
: > else
: > ps -au $USER | grep -i $1 |awk '{print "kill -9 " $1}'|sh
: > fi
: > }
:
: Match the process name against the CMD column only (not the PID, TTY,
: and TIME columns). Don't invoke both grep and awk. Use xargs instead
: of sh.
:
: cmd=$1
: ps -au $USER | awk "\$4 ~ /$cmd/ {print \$1}" | xargs kill -9
:
: --
: Kevin Rodgers

I would be remiss if I didn't mention that Linux and HP-UX both support
ps's with command matching syntax:

Linux:

ps -e -C cmd -o pid=

HP-UX

UNIX95=1 ps -e -C cmd -o pid=


Dan Mercer

Re: Unix Process name to PID

am 17.11.2004 10:44:33 von jcoll253

> ps -au $USER | grep -i $1 |awk '{print "kill -9 " $1}'|sh

I find this to be the best one, but I am getting the command trying to kill
the 'grep' process ID!, not the real process. Any Ideas?

Re: Unix Process name to PID

am 17.11.2004 11:04:15 von Stephane CHAZELAS

2004-11-17, 19:44(+10), James Collins:
>> ps -au $USER | grep -i $1 |awk '{print "kill -9 " $1}'|sh
>
> I find this to be the best one, but I am getting the command trying to kill
> the 'grep' process ID!, not the real process. Any Ideas?

It may also kill your "sh" or your awk.

ps -au "$USER" -o pid=,comm= | awk -v cmd="$1" '
$2 != "awk" && index(tolower($2), tolower(cmd)) {
pids=pids " " $1
}
END {
if (pids != "")
system("kill " pids "; (trap \"\" 1; sleep 5; kill -9 " pids \
"2> /dev/null) &")
}'

(untested) (use /usr/xpg4/bin/awk on Solaris)

It's generally not a good idea to send a KILL (9) signal to a
process.

--
Stephane

Re: Unix Process name to PID

am 17.11.2004 15:01:52 von gazelle

In article ,
Stephane CHAZELAS wrote:
>2004-11-17, 19:44(+10), James Collins:
>>> ps -au $USER | grep -i $1 |awk '{print "kill -9 " $1}'|sh
>>
>> I find this to be the best one, but I am getting the command trying to kill
>> the 'grep' process ID!, not the real process. Any Ideas?
>
>It may also kill your "sh" or your awk.
>
>ps -au "$USER" -o pid=,comm= | awk -v cmd="$1" '
> $2 != "awk" && index(tolower($2), tolower(cmd)) {
> pids=pids " " $1
> }
> END {
> if (pids != "")
> system("kill " pids "; (trap \"\" 1; sleep 5; kill -9 " pids \
> "2> /dev/null) &")
> }'
>
>(untested) (use /usr/xpg4/bin/awk on Solaris)

See what I mean? I believe some wise man stated upthread:

--- Start quote of wise man ---
Doing this in classical Unix using only shell tools is:

1) Ugly
2) Error-prone
3) A wheel that is frequenly re-invented

This is why most modern Unixes (e.g., Linux, Solaris) have special tools
designed for the job. Unfortunately, the portability police will tell us
that these tools aren't portable (which is true).
--- End quote of wise man ---

>It's generally not a good idea to send a KILL (9) signal to a
>process.

It's generally not a good idea to continuously re-invent dangerous wheels.

Re: Unix Process name to PID

am 19.11.2004 18:24:14 von Fahd

You guys are just jealous cos he picked me :)

--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking..."