Module: LinuxStat::Process

Defined in:
lib/linux_stat/process.rb

Overview

Shows various information about a process that is either running, sleeping, idle or a zombie.

Class Method Summary collapse

Class Method Details

.cmdlinesObject

Returns all the id of processes mapped with their cmdline info as a Hash. The cmdlines aren’t usually truncated like names, but they can contain arguments with the command.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/linux_stat/process.rb', line 57

def cmdlines
  h, i = {}, -1

  l = list
  count = l.length

  while(i += 1) < count
    x = l[i]

    begin
      cmdlines = IO.read("/proc/#{x}/cmdline").strip
      cmdlines.gsub!(?\u0000.freeze, ?\s.freeze)
      h.merge!( x => cmdlines)
    rescue StandardError
    end
  end
  h
end

.countObject

Counts and returns the total number of process running on the system.

The return type is Integer.



29
30
31
# File 'lib/linux_stat/process.rb', line 29

def count
  list.length
end

.idleObject

Returns all the id of processes that are idle. The return type is an Array of Integers.



120
121
122
123
124
# File 'lib/linux_stat/process.rb', line 120

def idle
  list.select { |x|
    LinuxStat::ProcFS.ps_state(x) == ?I.freeze
  }
end

.last_pidObject

Returns the last_pid of the system. It directly calls LS::ProcFS.last_pid

The return value is Integer, but if the status isn’t available, it will return nil



160
161
162
# File 'lib/linux_stat/process.rb', line 160

def last_pid
  LinuxStat::ProcFS.last_pid
end

.listObject

Returns the list of processes from /proc/.

The return type is an Array of Integers.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/linux_stat/process.rb', line 11

def list
  d = Dir['/proc/*'.freeze]
  ret, i = [], -1
  count = d.length

  while(i += 1) < count
    pid = File.split(d[i])[1]
    pid_i = pid.to_i
    ret << pid_i if pid_i.to_s == pid
  end

  ret
end

.namesObject

Returns all the id of processes mapped with their executable names (comm) as a Hash. The names can be truncated to TASK_COMM_LEN or (16 - 1 = 15) places.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/linux_stat/process.rb', line 36

def names
  h, i = {}, -1

  l = list
  count = l.length

  while(i += 1) < count
    x = l[i]

    begin
      h.merge!( x => IO.read("/proc/#{x}/comm").strip)
    rescue StandardError
    end
  end
  h
end

.runningObject

Returns all the id of processes that are running. The return type is an Array of Integers.



138
139
140
141
142
# File 'lib/linux_stat/process.rb', line 138

def running
  list.select { |x|
    LinuxStat::ProcFS.ps_state(x) == ?R.freeze
  }
end

.sleepingObject

Returns all the id of processes that are sleeping. The return type is an Array of Integers.



111
112
113
114
115
# File 'lib/linux_stat/process.rb', line 111

def sleeping
  list.select { |x|
    LinuxStat::ProcFS.ps_state(x) == ?S.freeze
  }
end

.stoppedObject

Returns all the id of processes that are stopped. The return type is an Array of Integers.



147
148
149
150
151
152
# File 'lib/linux_stat/process.rb', line 147

def stopped
  list.select { |x|
    v = LinuxStat::ProcFS.ps_state(x)
    v == ?T.freeze || v == ?t.freeze
  }
end

.typesObject

Returns all the id of processes mapped with their status as a Hash.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/linux_stat/process.rb', line 78

def types
  h, i = {}, -1

  l = list
  count = l.length

  while(i += 1) < count
    x = l[i]

    begin
      h.merge!(x =>
        case LinuxStat::ProcFS.ps_state(x)
          when ?S.freeze then :sleeping
          when ?I.freeze then :idle
          when ?Z.freeze then :zombie
          when ?R.freeze then :running
          when ?T.freeze then :stopped
          when ?X.freeze then :dead
          when ?D.freeze then :sleeping
          when ?t.freeze then :stopped
          else :unknown
        end
      )
    rescue StandardError
    end
  end

  h
end

.zombieObject

Returns all the id of processes that are zombies. The return type is an Array of Integers.



129
130
131
132
133
# File 'lib/linux_stat/process.rb', line 129

def zombie
  list.select { |x|
    LinuxStat::ProcFS.ps_state(x) == ?Z.freeze
  }
end