Class: Knj::Unix_proc

Inherits:
Object show all
Defined in:
lib/knj/unix_proc.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Unix_proc

Returns a new instance of Unix_proc.



77
78
79
# File 'lib/knj/unix_proc.rb', line 77

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



2
3
4
# File 'lib/knj/unix_proc.rb', line 2

def data
  @data
end

Class Method Details

.find_selfObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/knj/unix_proc.rb', line 62

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

.list(args = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/knj/unix_proc.rb', line 17

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

.spawn(data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/knj/unix_proc.rb', line 5

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

Instance Method Details

#[](key) ⇒ Object



85
86
87
88
# File 'lib/knj/unix_proc.rb', line 85

def [](key)
  raise "No such data: #{key}" if !@data.key?(key)
  return @data[key]
end

#killObject



90
91
92
# File 'lib/knj/unix_proc.rb', line 90

def kill
  Process.kill(9, @data["pid"].to_i)
end

#update_data(data) ⇒ Object



81
82
83
# File 'lib/knj/unix_proc.rb', line 81

def update_data(data)
  @data = data
end