Kill process by his name

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

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

Kind regards

  robert

···

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

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

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?

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

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

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}

Philip Hallstrom wrote:

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

Wonderful! Thank you :slight_smile:

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

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

:slight_smile:

Kind regards

  robert

···

On 26.09.2006 20:52, Suraj N. Kurapati wrote: