Kill process by his name

Discussion in 'Ruby' started by David Corticchiato, Sep 26, 2006.

  1. 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/.
     
    David Corticchiato, Sep 26, 2006
    #1
    1. Advertising

  2. 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
     
    Robert Klemme, Sep 26, 2006
    #2
    1. Advertising

  3. 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/.
     
    Eero Saynatkari, Sep 26, 2006
    #3
  4. -----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 N. Kurapati, Sep 26, 2006
    #4
  5. -----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-----
     
    Suraj N. Kurapati, Sep 26, 2006
    #5
  6. >> 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, Sep 26, 2006
    #6
  7. -----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-----
     
    Suraj N. Kurapati, Sep 26, 2006
    #7
  8. 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
     
    Robert Klemme, Sep 27, 2006
    #8
  9. 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?
     
    Jonas Hartmann, Sep 27, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. drodrig
    Replies:
    4
    Views:
    13,888
    Roger Upole
    Aug 13, 2006
  2. stef mientki

    howto kill a windows process by name ?

    stef mientki, Oct 5, 2007, in forum: Python
    Replies:
    0
    Views:
    371
    stef mientki
    Oct 5, 2007
  3. stef mientki
    Replies:
    0
    Views:
    749
    stef mientki
    Oct 6, 2007
  4. Agustin Ramos

    Kill Process by it's name

    Agustin Ramos, Jan 17, 2008, in forum: Ruby
    Replies:
    5
    Views:
    230
    luis lopez
    Jan 17, 2008
  5. sanjayayogi
    Replies:
    3
    Views:
    135
    Martin DeMello
    Apr 4, 2009
Loading...

Share This Page