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
路路路
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:
--
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
===============================================================================