easy way to check if a pid is running?

Discussion in 'Ruby' started by George Mochrie, Aug 25, 2004.

  1. hi

    A quick question:

    Is there an easy way to check if a process is running given it's PID? There
    seems to be nothing like Process.exists?(pid). If I was root I could use
    Process.kill(0,pid) to test, but the program needs to run as a plain user. Do
    I have to parse the output from ps -e?

    Thanks in advance :)
    --
    George Mochrie
    aka Drasil
    ICQ# 84423409
    George Mochrie, Aug 25, 2004
    #1
    1. Advertising

  2. George Mochrie

    Gennady Guest

    George Mochrie wrote:
    > hi
    >
    > A quick question:
    >
    > Is there an easy way to check if a process is running given it's PID? There
    > seems to be nothing like Process.exists?(pid). If I was root I could use
    > Process.kill(0,pid) to test, but the program needs to run as a plain user. Do


    Not only root can do kill(0, pid), any user can send signal (or 0) to
    his/her own processes.

    On systems that have /proc you can also check for presence of a
    directory named like pid there.

    > I have to parse the output from ps -e?
    >
    > Thanks in advance :)


    Gennady.
    Gennady, Aug 25, 2004
    #2
    1. Advertising

  3. many thanks, /proc will do fine :)

    On Wednesday 25 August 2004 22:01, Gennady wrote:
    > George Mochrie wrote:
    > > hi
    > >
    > > A quick question:
    > >
    > > Is there an easy way to check if a process is running given it's PID?
    > > There seems to be nothing like Process.exists?(pid). If I was root I
    > > could use Process.kill(0,pid) to test, but the program needs to run as a
    > > plain user. Do

    >
    > Not only root can do kill(0, pid), any user can send signal (or 0) to
    > his/her own processes.
    >
    > On systems that have /proc you can also check for presence of a
    > directory named like pid there.
    >
    > > I have to parse the output from ps -e?
    > >
    > > Thanks in advance :)

    >
    > Gennady.


    --
    George Mochrie
    aka Drasil
    ICQ# 84423409
    George Mochrie, Aug 25, 2004
    #3
  4. George Mochrie

    Mark Hubbart Guest

    On Aug 25, 2004, at 2:01 PM, Gennady wrote:

    > George Mochrie wrote:
    >> hi
    >> A quick question:
    >> Is there an easy way to check if a process is running given it's PID?
    >> There seems to be nothing like Process.exists?(pid). If I was root I
    >> could use Process.kill(0,pid) to test, but the program needs to run
    >> as a plain user. Do

    >
    > Not only root can do kill(0, pid), any user can send signal (or 0) to
    > his/her own processes.
    >
    > On systems that have /proc you can also check for presence of a
    > directory named like pid there.


    You can't send a signal, but you get a different error:

    mark@eMac% irb --prompt xmp

    Process.kill 0, 1
    Errno::EPERM: Operation not permitted
    from (irb):2:in `kill'
    from (irb):2

    Process.kill 0, 512
    Errno::ESRCH: No such process
    from (irb):3:in `kill'
    from (irb):3

    Process.kill 0, 419
    ==>1

    process 1 is owned by root, 512 doesn't exist, 419 was mine.

    So, Process.exists? could be:

    def Process.exists?(pid)
    Process.kill 0, pid
    return true
    rescue Errno::ESRCH
    return false
    rescue Exception
    return true
    end
    ==>nil

    Process.exists? 1
    ==>true

    Process.exists? 512
    ==>false

    Process.exists? 419
    ==>true

    >> I have to parse the output from ps -e?
    >> Thanks in advance :)

    >
    > Gennady.
    >
    Mark Hubbart, Aug 25, 2004
    #4
  5. George Mochrie

    Guest

    On Thu, 26 Aug 2004, George Mochrie wrote:

    > hi
    >
    > A quick question:
    >
    > Is there an easy way to check if a process is running given it's PID? There
    > seems to be nothing like Process.exists?(pid). If I was root I could use
    > Process.kill(0,pid) to test, but the program needs to run as a plain user. Do
    > I have to parse the output from ps -e?
    >
    > Thanks in advance :)
    > --
    > George Mochrie
    > aka Drasil
    > ICQ# 84423409



    from my util module:

    def alive? pid
    #{{{
    pid = Integer("#{ pid }")
    begin
    Process.kill 0, pid
    true
    rescue Errno::ESRCH
    false
    end
    #}}}
    end


    the module

    require 'pathname'
    require 'socket'
    module Util
    #{{{
    class << self
    def export sym
    #{{{
    sym = "#{ sym }".intern
    module_function sym
    public sym
    #}}}
    end
    def append_features c
    #{{{
    super
    c.extend Util
    #}}}
    end
    end
    def mcp obj
    #{{{
    Marshal.load(Marshal.dump(obj))
    #}}}
    end
    export 'mcp'
    def klass
    #{{{
    self.class
    #}}}
    end
    export 'klass'
    def realpath path
    #{{{
    path = File.expand_path "#{ path }"
    begin
    Pathname.new(path).realpath.to_s
    rescue Errno::ENOENT, Errno::ENOTDIR
    path
    end
    #}}}
    end
    export 'realpath'
    def hashify(*hashes)
    #{{{
    hashes.inject(accum={}){|accum,hash| accum.update hash}
    #}}}
    end
    export 'hashify'
    def getopt opt, hash
    #{{{
    opt_s = "#{ opt }"
    hash[opt] || hash[opt_s] || hash[opt_s.intern]
    #}}}
    end
    export 'getopt'
    def alive? pid
    #{{{
    pid = Integer("#{ pid }")
    begin
    Process.kill 0, pid
    true
    rescue Errno::ESRCH
    false
    end
    #}}}
    end
    export 'alive?'
    def maim(pid, opts = {})
    #{{{
    sigs = getopt('signals', opts) || %w(SIGTERM SIGQUIT SIGKILL)
    suspend = getopt('suspend', opts) || 4
    pid = Integer("#{ pid }")
    sigs.each do |sig|
    begin
    Process.kill(sig, pid)
    rescue Errno::ESRCH
    return nil
    end
    sleep 0.2
    unless alive?(pid)
    break
    else
    sleep suspend
    end
    end
    not alive?(pid)
    #}}}
    end
    export 'maim'
    def timestamp time = Time.now
    #{{{
    usec = "#{ time.usec }"
    usec << ('0' * (6 - usec.size)) if usec.size < 6
    time.strftime('%Y-%m-%d %H:%M:%S.') << usec
    #}}}
    end
    export 'timestamp'
    def stamptime string, local = true
    #{{{
    string = "#{ string }"
    pat = %r/^\s*(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d).(\d\d\d\d\d\d)\s*$/o
    match = pat.match string
    raise ArgumentError, "<#{ string.inspect }>" unless match
    yyyy,mm,dd,h,m,s,u = match.to_a[1..-1].map{|m| m.to_i}
    if local
    Time.local yyyy,mm,dd,h,m,s,u
    else
    Time.gm yyyy,mm,dd,h,m,s,u
    end
    #}}}
    end
    export 'stamptime'
    def escape! s, char, esc
    #{{{
    re = %r/([#{0x5c.chr << esc}]*)#{char}/
    s.gsub!(re) do
    (($1.size % 2 == 0) ? ($1 << esc) : $1) + char
    end
    #}}}
    end
    export 'escape!'
    def escape s, char, esc
    #{{{
    ss = "#{ s }"
    escape! ss, char, esc
    ss
    #}}}
    end
    export 'escape'
    def fork(*args, &block)
    #{{{
    begin
    verbose = $VERBOSE
    $VERBOSE = nil
    Process.fork(*args, &block)
    ensure
    $VERBOSE = verbose
    end
    #}}}
    end
    export 'fork'
    def exec(*args, &block)
    #{{{
    begin
    verbose = $VERBOSE
    $VERBOSE = nil
    Kernel.exec(*args, &block)
    ensure
    $VERBOSE = verbose
    end
    #}}}
    end
    export 'exec'
    def hostname
    #{{{
    @__hostname__ ||= Socket::gethostname
    #}}}
    end
    export 'hostname'
    def host
    #{{{
    @__host__ ||= Socket::gethostname.gsub(%r/\..*$/o,'')
    #}}}
    end
    export 'host'
    def emsg e
    #{{{
    "<#{ e.class }> - <#{ e.message }>"
    #}}}
    end
    export 'emsg'
    def btrace e
    #{{{
    (e.backtrace or []).join("\n")
    #}}}
    end
    export 'btrace'
    def errmsg e
    #{{{
    emsg(e) << "\n" << btrace(e)
    #}}}
    end
    export 'errmsg'
    def erreq a, b
    #{{{
    a.class == b.class and
    a.message == b.message and
    a.backtrace == b.backtrace
    #}}}
    end
    export 'erreq'
    #}}}
    end # class Util


    regards

    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | A flower falls, even though we love it;
    | and a weed grows, even though we do not love it.
    | --Dogen
    ===============================================================================
    , Aug 25, 2004
    #5
  6. the perfect solution... thanks :)

    On Wednesday 25 August 2004 22:26, Mark Hubbart wrote:
    > On Aug 25, 2004, at 2:01 PM, Gennady wrote:
    > > George Mochrie wrote:
    > >> hi
    > >> A quick question:
    > >> Is there an easy way to check if a process is running given it's PID?
    > >> There seems to be nothing like Process.exists?(pid). If I was root I
    > >> could use Process.kill(0,pid) to test, but the program needs to run
    > >> as a plain user. Do

    > >
    > > Not only root can do kill(0, pid), any user can send signal (or 0) to
    > > his/her own processes.
    > >
    > > On systems that have /proc you can also check for presence of a
    > > directory named like pid there.

    >
    > You can't send a signal, but you get a different error:
    >
    > mark@eMac% irb --prompt xmp
    >
    > Process.kill 0, 1
    > Errno::EPERM: Operation not permitted
    > from (irb):2:in `kill'
    > from (irb):2
    >
    > Process.kill 0, 512
    > Errno::ESRCH: No such process
    > from (irb):3:in `kill'
    > from (irb):3
    >
    > Process.kill 0, 419
    > ==>1
    >
    > process 1 is owned by root, 512 doesn't exist, 419 was mine.
    >
    > So, Process.exists? could be:
    >
    > def Process.exists?(pid)
    > Process.kill 0, pid
    > return true
    > rescue Errno::ESRCH
    > return false
    > rescue Exception
    > return true
    > end
    > ==>nil
    >
    > Process.exists? 1
    > ==>true
    >
    > Process.exists? 512
    > ==>false
    >
    > Process.exists? 419
    > ==>true
    >
    > >> I have to parse the output from ps -e?
    > >> Thanks in advance :)

    > >
    > > Gennady.


    --
    George Mochrie
    aka Drasil
    ICQ# 84423409
    George Mochrie, Aug 26, 2004
    #6
    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. Stephen Toledo-Brown
    Replies:
    1
    Views:
    493
  2. David Hirschfield
    Replies:
    3
    Views:
    3,285
    Thomas Guettler
    Feb 3, 2006
  3. Lars =?iso-8859-15?Q?Gust=E4bel?=

    Re: Best way to determine if a certain PID is still running

    Lars =?iso-8859-15?Q?Gust=E4bel?=, Feb 3, 2006, in forum: Python
    Replies:
    2
    Views:
    450
    MrJean1
    Feb 4, 2006
  4. kathy
    Replies:
    3
    Views:
    737
  5. bukzor
    Replies:
    4
    Views:
    392
    bukzor
    May 21, 2008
Loading...

Share This Page