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.



15
16
17
# File 'lib/linux_stat/process.rb', line 15

def count
	list.count
end

.idleObject

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



63
64
65
66
67
68
69
70
71
# File 'lib/linux_stat/process.rb', line 63

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.



6
7
8
9
10
11
# File 'lib/linux_stat/process.rb', line 6

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.



20
21
22
23
24
25
26
27
28
# File 'lib/linux_stat/process.rb', line 20

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.



87
88
89
90
91
92
93
94
95
# File 'lib/linux_stat/process.rb', line 87

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.



51
52
53
54
55
56
57
58
59
# File 'lib/linux_stat/process.rb', line 51

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.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/linux_stat/process.rb', line 31

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.



75
76
77
78
79
80
81
82
83
# File 'lib/linux_stat/process.rb', line 75

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