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.



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

def idle
	list.select { |x|
		begin
			IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?I.freeze
		rescue StandardError
			false
		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 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.



150
151
152
153
154
155
156
157
158
# File 'lib/linux_stat/process.rb', line 150

def running
	list.select { |x|
		begin
			IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?R.freeze
		rescue StandardError
			false
		end
	}
end

.sleepingObject

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



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

def sleeping
	list.select { |x|
		begin
			IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?S.freeze
		rescue StandardError
			false
		end
	}
end

.stoppedObject

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



163
164
165
166
167
168
169
170
171
172
# File 'lib/linux_stat/process.rb', line 163

def stopped
	list.select { |x|
		begin
			v = IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip
			v == ?T.freeze || v == ?t.freeze
		rescue StandardError
			false
		end
	}
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 IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip
					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.



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

def zombie
	list.select { |x|
		begin
			IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?Z.freeze
		rescue StandardError
			false
		end
	}
end