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.

On 26.09.2006 17:14, David C. 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

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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Eero S. wrote:

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

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}

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Philip H. 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}

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

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

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

:slight_smile:

Kind regards

robert

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Suraj N. Kurapati wrote:

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

David C. 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?