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.



26
27
28
# File 'lib/linux_stat/process.rb', line 26

def count
	list.length
end

.idleObject

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



93
94
95
96
97
98
99
100
101
# File 'lib/linux_stat/process.rb', line 93

def idle
	list.select { |x|
		begin
			IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?I.freeze
		rescue StandardError
			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
14
15
16
17
18
19
20
# File 'lib/linux_stat/process.rb', line 8

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 names as a Hash.



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

def names
	h, i = {}, -1

	l = list
	count = l.length

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

		begin
			h.merge!( x => IO.foreach("/proc/#{x}/status").first.split[1])
		rescue StandardError
		end
	end
	h
end

.runningObject

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



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

def running
	list.select { |x|
		begin
			IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?R.freeze
		rescue StandardError
			nil
		end
	}
end

.sleepingObject

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



80
81
82
83
84
85
86
87
88
# File 'lib/linux_stat/process.rb', line 80

def sleeping
	list.select { |x|
		begin
			IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?S.freeze
		rescue StandardError
			nil
		end
	}
end

.typesObject

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



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

def types
	h, i = {}, -1

	l = list
	count = l.length

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

		begin
			h.merge!(x =>
				case IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0]
					when ?S.freeze then :sleeping
					when ?I.freeze then :idle
					when ?Z.freeze then :zombie
					when ?R.freeze then :running
					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.



106
107
108
109
110
111
112
113
114
# File 'lib/linux_stat/process.rb', line 106

def zombie
	list.select { |x|
		begin
			IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?Z.freeze
		rescue StandardError
			nil
		end
	}
end