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

.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.



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

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.



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



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

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.



122
123
124
125
126
127
128
129
130
# File 'lib/linux_stat/process.rb', line 122

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.



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

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.



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

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.



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

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