easy way to check if a pid is running?

G

George Mochrie

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 :)
 
G

Gennady

George said:
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.
 
G

George Mochrie

many thanks, /proc will do fine :)

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.


Gennady.
 
M

Mark Hubbart

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
 
A

Ara.T.Howard

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


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

George Mochrie

the perfect solution... thanks :)

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
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top