Module: LinuxStat::Process

Defined in:
lib/linux_stat/process.rb

Class Method Summary collapse

Class Method Details

.countObject

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

The return type is Integer.



19
20
21
# File 'lib/linux_stat/process.rb', line 19

def count
  list.count
end

.idleObject

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



71
72
73
74
75
76
77
78
79
# File 'lib/linux_stat/process.rb', line 71

def idle
  list.select { |x|
    begin
      IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?I
    rescue Exception
      nil
    end
  }
end

.listObject

Returns the list of processes from /proc/.

The return type is an Array of Integers.



8
9
10
11
12
13
# File 'lib/linux_stat/process.rb', line 8

def list
  Dir['/proc/*'].select! { |x|
    pid = File.split(x)[1]
    pid.to_i.to_s == pid
  }.map! { |x| File.split(x)[-1].to_i }
end

.namesObject

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



25
26
27
28
29
30
31
32
33
# File 'lib/linux_stat/process.rb', line 25

def names
  list.reduce({}) { |h, x|
    begin
      h.merge!( x => IO.foreach(File.join('/proc', x.to_s, 'status')).first.split[1] )
    rescue Exception
      h
    end
  }
end

.runningObject

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



97
98
99
100
101
102
103
104
105
# File 'lib/linux_stat/process.rb', line 97

def running
  list.select { |x|
    begin
      IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?R
    rescue Exception
      nil
    end
  }
end

.sleepingObject

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



58
59
60
61
62
63
64
65
66
# File 'lib/linux_stat/process.rb', line 58

def sleeping
  list.select { |x|
    begin
      IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?S
    rescue Exception
      nil
    end
  }
end

.typesObject

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



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

def types
  list.reduce({}) { |h, x|
    begin
      h.merge!(x =>
        case IO.read(File.join('/proc', x.to_s, 'stat')).split[2]
          when ?S.freeze then :sleeping
          when ?I.freeze then :idle
          when ?Z.freeze then :zombie
          when ?R.freeze then :running
          else :unknown
        end
      )
    rescue Exception
      h
    end
  }
end

.zombieObject

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



84
85
86
87
88
89
90
91
92
# File 'lib/linux_stat/process.rb', line 84

def zombie
  list.select { |x|
    begin
      IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?Z
    rescue Exception
      nil
    end
  }
end