Sha256: aa6c5b40c1c9f79b1d12c6f99a56b75927bed3d29173c4759a4f4aa6b4862b43

Contents?: true

Size: 1.93 KB

Versions: 12

Compression:

Stored size: 1.93 KB

Contents

class Knj::Unix_proc
  attr_reader :data
  @procs = {}
  
  def self.spawn(data)
    proc_ele = @procs[data["pid"]]
    
    if proc_ele
      proc_ele.update_data(data)
    else
      @procs[data["pid"]] = Knj::Unix_proc.new(data)
    end
    
    return @procs[data["pid"]]
  end
  
  def self.list(args = {})
    cmdstr = "ps aux"
    grepstr = ""
    
    if args["grep"]
      grepstr = "grep #{Knj::Strings.unixsafe(args["grep"])}"
      cmdstr << " | #{grepstr}"
    end
    
    ret = []
    res = Knj::Os.shellcmd(cmdstr)
    
    res.scan(/^(\S+)\s+([0-9]+)\s+([0-9.]+)\s+([0-9.]+)\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+ (.+)($|\n)/) do |match|
      pid = match[1]
      
      data = {
        "user" => match[0],
        "pid" => pid,
        "cpu_last" => match[2],
        "ram_last" => match[3],
        "cmd" => match[4],
        "app" => File.basename(match[4])
      }
      
      next if (!args.key?("ignore_self") or args["ignore_self"]) and match[1].to_i == $$.to_i
      next if grepstr.length > 0 and match[4].index(grepstr) != nil #dont return current process.
      
      if args.key?("pids")
        found = false
        args["pids"].each do |pid_given|
          if pid_given.to_s == pid.to_s
            found = true
            break
          end
        end
        
        next if !found
      end
      
      ret << Knj::Unix_proc.spawn(data)
    end
    
    return ret
  end
  
  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
        proc_find = proc_ele
        break
      end
    end
    
    return proc_find
  end
  
  def initialize(data)
    @data = data
  end
  
  def update_data(data)
    @data = data
  end
  
  def [](key)
    raise "No such data: #{key}" if !@data.key?(key)
    return @data[key]
  end
  
  def kill
    Process.kill(9, @data["pid"].to_i)
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
knjrbfw-0.0.21 lib/knj/unix_proc.rb
knjrbfw-0.0.20 lib/knj/unix_proc.rb
knjrbfw-0.0.19 lib/knj/unix_proc.rb
knjrbfw-0.0.18 lib/knj/unix_proc.rb
knjrbfw-0.0.17 lib/knj/unix_proc.rb
knjrbfw-0.0.16 lib/knj/unix_proc.rb
knjrbfw-0.0.15 lib/knj/unix_proc.rb
knjrbfw-0.0.14 lib/knj/unix_proc.rb
knjrbfw-0.0.13 lib/knj/unix_proc.rb
knjrbfw-0.0.12 lib/knj/unix_proc.rb
knjrbfw-0.0.11 lib/knj/unix_proc.rb
knjrbfw-0.0.10 lib/knj/unix_proc.rb