Kill process by his name

D

David Corticchiato

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

Robert Klemme

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

Eero Saynatkari

David said:
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.
 
S

Suraj N. Kurapati

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

Eero said:
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-----
 
S

Suraj N. Kurapati

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
P

Philip Hallstrom

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

Suraj N. Kurapati

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

Philip said:
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-----
 
R

Robert Klemme

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\n", "bar"]

:)

Kind regards

robert
 
J

Jonas Hartmann

David said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top