Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Kill process by his name

Reply
Thread Tools

Kill process by his name

 
 
David Corticchiato
Guest
Posts: n/a
 
      09-26-2006
Hi

is it possible to kill a process by his name without using directly a
program of the OS used ? I mean without using system(...) or `...`.

David.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-26-2006
On 26.09.2006 17:14, David Corticchiato wrote:
> is it possible to kill a process by his name without using directly a
> program of the OS used ? I mean without using system(...) or `...`.


I doubt it. For completeness reasons: That would be on what OS?

Kind regards

robert
 
Reply With Quote
 
 
 
 
Eero Saynatkari
Guest
Posts: n/a
 
      09-26-2006
David Corticchiato wrote:
> Hi
>
> is it possible to kill a process by his name without using directly a
> program of the OS used ? I mean without using system(...) or `...`.


No. That is not generally supported even on POSIX systems
(hence the killall command). You can parse the process list,
grab the PID and issue a kill on it.


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Suraj N. Kurapati
Guest
Posts: n/a
 
      09-26-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Eero Saynatkari wrote:
> David Corticchiato wrote:
>> is it possible to kill a process by his name without using directly a
>> program of the OS used ? I mean without using system(...) or `...`.

>
> No. That is not generally supported even on POSIX systems
> (hence the killall command). You can parse the process list,
> grab the PID and issue a kill on it.


Here is an example. Suppose you wanted to kill all existing
processes of the current program:

pidList = `ps -C #{File.basename $0} -o pid`.split.map! {|s| s.to_i}
pidList.shift # discard header
pidList.reject! {|pid| pid == $$}

pidList.each do |pid|
Process.kill :SIGTERM, pid
end

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFFGWv0mV9O7RYnKMcRAgTbAKCkt5t3NO5U/9lzWmGGZdaWUKjVZwCgs6vm
jMtlv5H1cVSxowRoGJITXA4=
=/TEd
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Suraj N. Kurapati
Guest
Posts: n/a
 
      09-26-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Suraj N. Kurapati wrote:
> Eero Saynatkari wrote:
>> You can parse the process list, grab the PID and issue a kill
>> on it.

>
> Here is an example. Suppose you wanted to kill all existing
> processes of the current program:
>
> pidList = `ps -C #{File.basename $0} -o pid`.split.map! {|s| s.to_i}
> pidList.shift # discard header
> pidList.reject! {|pid| pid == $$}
>
> pidList.each do |pid|
> Process.kill :SIGTERM, pid
> end


Here's a better way to write the above:

pidList = `ps -C #{File.basename $0} -o pid`.
split[1..-1]. # discard header from `ps` output
map! {|s| s.to_i}.
reject! {|pid| pid == $$}

pidList.each do |pid|
Process.kill :SIGTERM, pid
end
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFFGW3JmV9O7RYnKMcRAjodAJ0aWPy7fo8ReURX4SVPpj J8DXWanQCfd2+b
ftgHBiEredpMSOatibeylZI=
=C0wf
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Philip Hallstrom
Guest
Posts: n/a
 
      09-26-2006
>> Here is an example. Suppose you wanted to kill all existing
>> processes of the current program:
>>
>> pidList = `ps -C #{File.basename $0} -o pid`.split.map! {|s| s.to_i}
>> pidList.shift # discard header
>> pidList.reject! {|pid| pid == $$}
>>
>> pidList.each do |pid|
>> Process.kill :SIGTERM, pid
>> end

>
> Here's a better way to write the above:
>
> pidList = `ps -C #{File.basename $0} -o pid`.
> split[1..-1]. # discard header from `ps` output
> map! {|s| s.to_i}.
> reject! {|pid| pid == $$}
>
> pidList.each do |pid|
> Process.kill :SIGTERM, pid
> end


Couldn't you take it all the way to this?

`ps -C #{File.basename $0} -o pid h`. # no header
map! {|s| s.to_i}.
reject! {|pid| pid == $$}.
each {|pid| Process.kill :SIGTERM, pid}



 
Reply With Quote
 
Suraj N. Kurapati
Guest
Posts: n/a
 
      09-26-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Philip Hallstrom wrote:
> Couldn't you take it all the way to this?


Wonderful! Thank you

>
> `ps -C #{File.basename $0} -o pid h`. # no header
> map! {|s| s.to_i}.


I'm a bit confused here. String class does not have a #map or #map!
method, so how is this working?

> reject! {|pid| pid == $$}.
> each {|pid| Process.kill :SIGTERM, pid}


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFFGXchmV9O7RYnKMcRAvGwAJ9iJyPvoMchkvDxumcTZ1 7kESsYlACgk6f+
TwvEw+nozO86WU+fZwZBf7g=
=9a9N
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-27-2006
On 26.09.2006 20:52, Suraj N. Kurapati wrote:
> I'm a bit confused here. String class does not have a #map or #map!
> method, so how is this working?


You sure about that?

>> "foo\nbar".map {|x| x}

=> ["foo\n", "bar"]



Kind regards

robert
 
Reply With Quote
 
Jonas Hartmann
Guest
Posts: n/a
 
      09-27-2006
David Corticchiato wrote:
> Hi
>
> is it possible to kill a process by his name without using directly a
> program of the OS used ? I mean without using system(...) or `...`.
>
> David.
>

isnt this generally a bad idea?
process names are mostly not unique right?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Your true traveler finds boredom rather agreeable than painful. It isthe symbol of his liberty -- his excessive freedom. He accepts his boredom,when it comes, not merely philosophically, but almost with pleasure. senthilind@gmail.com Computer Support 0 03-02-2008 08:23 AM
Re: howto kill a windows process by name ? stef mientki Python 0 10-05-2007 11:30 PM
howto kill a windows process by name ? stef mientki Python 0 10-05-2007 09:40 PM
Kill process based on window name (win32) drodrig Python 4 08-13-2006 07:37 PM



Advertisments