[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Kill process by his name

David Corticchiato

9/26/2006 3:15:00 PM

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-....

8 Answers

Robert Klemme

9/26/2006 3:20:00 PM

0

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

e

9/26/2006 4:02:00 PM

0

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-....

Suraj Kurapati

9/26/2006 6:05:00 PM

0

-----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-----

Suraj Kurapati

9/26/2006 6:14:00 PM

0

-----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)

iD8DBQFFGW3JmV9O7RYnKMcRAjodAJ0aWPy7fo8ReURX4SVPpjJ8DXWanQCfd2+b
ftgHBiEredpMSOatibeylZI=
=C0wf
-----END PGP SIGNATURE-----

Philip Hallstrom

9/26/2006 6:29:00 PM

0

Suraj Kurapati

9/26/2006 6:53:00 PM

0

-----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)

iD8DBQFFGXchmV9O7RYnKMcRAvGwAJ9iJyPvoMchkvDxumcTZ17kESsYlACgk6f+
TwvEw+nozO86WU+fZwZBf7g=
=9a9N
-----END PGP SIGNATURE-----

Robert Klemme

9/27/2006 9:29:00 AM

0

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

Jonas Hartmann

9/27/2006 10:14:00 AM

0

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?