lib/knj/unix_proc.rb in knjrbfw-0.0.48 vs lib/knj/unix_proc.rb in knjrbfw-0.0.49

- old
+ new

@@ -1,14 +1,16 @@ +#Requires the 'wref'-gem. require "wref" #This class handels various stuff regarding Unix-processes. class Knj::Unix_proc attr_reader :data PROCS = Wref_map.new MUTEX = Mutex.new + #Spawns a process if it doesnt already exist in the wrap-map. def self.spawn(data) pid = data["pid"].to_i begin proc_ele = PROCS[pid] @@ -19,10 +21,11 @@ end return proc_ele end + #Returns an array with (or yields if block given) Unix_proc. Hash-arguments as 'grep'. def self.list(args = {}) cmdstr = "ps aux" grepstr = "" if args["grep"] @@ -78,37 +81,57 @@ return ret end end end + #Returns the "Knj::Unix_proc" for the current process. def self.find_self procs = Knj::Unix_proc.list("ignore_self" => false) pid_find = Process.pid proc_find = false procs.each do |proc_ele| - if proc_ele["pid"].to_s == pid_find.to_s + if proc_ele["pid"].to_i == pid_find.to_i proc_find = proc_ele break end end return proc_find end + #Return true if the given PID is running. + def self.pid_running?(pid) + begin + Process.getpgid(pid) + return true + rescue Errno::ESRCH + return false + end + end + + #Initializes various data for a Unix_proc-object. This should not be called manually but through "Unix_proc.list". def initialize(data) @data = data end + #Updates the data. This should not be called manually, but is exposed because of various code in "Unix_proc.list". def update_data(data) @data = data end + #Returns a key from the data or raises an error. def [](key) raise "No such data: #{key}" if !@data.key?(key) return @data[key] end + #Kills the process. def kill + Process.kill("TERM", @data["pid"].to_i) + end + + #Kills the process with 9. + def kill! Process.kill(9, @data["pid"].to_i) end end \ No newline at end of file