1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Provides information on currently running processes, by examining files in the C{/proc} filesystem.
19 (So it doesn't work on OS X, for example.) The C{processes} function returns a list of C{Process}
20 objects. Each C{Process} object reveals information derived from C{/proc}, identifies
21 parent and child processes, and can be used to send signals to the process.
22 """
23
24 import os
25 import os.path
26 import pickle
27
29 """Returns a list of process objects based on the current contents of C{/proc}.
30 Of course the list is stale as soon as it is formed. In particular, a C{Process}
31 object in the list may correspond to a process that has terminated by the time
32 you use the object.
33 """
34 processes = []
35 for file in os.listdir('/proc'):
36 if file.isdigit():
37 processes.append(Process(int(file)))
38 return processes
39
41 """A C{Process} object represents a process with a particular PID. The process may or may not
42 be running when the C{Process} object is used. It is conceivable that the C{Process} object
43 does not represent the same process that was identified by the PID when the C{Process} object
44 was created.
45 """
46
47 __pid = None
48 __parent = None
49 __commandline = None
50 __env = None
51 __status = None
52
54 """Creates a C{Process} object for a given C{pid}. For internal use only.
55 """
56 self.__pid = pid
57
59 """Returns a string describing this C{Process}.
60 """
61 return 'Process(%s)' % self.__pid
62
64 """Ranks C{Process}es by PID.
65 """
66 return self.__pid - other.___pid
67
70
72
73 self._ensure_status_file_read()
74 x = self.commandline
75 x = self.env
76
82
84 """
85 """
86 subtree = []
87 self._add_children_recursively(subtree, processes())
88 return subtree
89
100
102 size = None
103 if self._ensure_status_file_read():
104 size = self.__status.get('VmSize', None)
105 if size is not None:
106 space = size.find(' ')
107 assert size[space + 1:].lower() == 'kb'
108 size = int(size[:space])
109 size *= 1024
110 return size
111
113 rss = None
114 if self._ensure_status_file_read():
115 rss = self.__status.get('VmRSS', None)
116 if rss is not None:
117 space = rss.find(' ')
118 assert rss[space + 1:].lower() == 'kb'
119 rss = int(rss[:space])
120 rss *= 1024
121 return rss
122
131
133 if not self.__env:
134 env_map = self._strings_file('environ')
135 if env_map:
136 self.__env = {}
137 for key_value_string in env_map:
138 eq = key_value_string.find('=')
139 key = key_value_string[:eq].strip()
140 value = key_value_string[eq + 1:].strip()
141 if key:
142 self.__env[key] = value
143 return self.__env
144
145 pid = property(lambda self: self.__pid, doc = 'The PID of this C{Process}.')
146 parent = property(_parent, doc = 'The parent of this C{Process}. Returns a C{Process} object.')
147 descendents = property(_descendents, doc = 'A list containing C{Process} objects corresponding to the descendents of this C{Process}, (children, grandchildren, etc.)')
148 state = property(_state, doc = 'The state of this C{Process}.')
149 size = property(_size, doc = 'The VM size of this C{Process}.')
150 rss = property(_rss, doc = 'The VM RSS of this C{Process}.')
151 commandline = property(_commandline, doc = 'The command-line used to create this C{Process}.')
152 env = property(_env, doc = 'A map describing the environment in effect during the creation of this C{Process}.')
153
154 - def kill(self, signal = None):
155 """Send the indicated C{signal} to this process.
156 """
157 if signal:
158 os.system('kill -%s %s' % (signal, self.pid))
159 else:
160 os.system('kill %s' % (self.pid))
161
163 exists = True
164 if self.__status is None:
165 self.__status = {}
166 try:
167 status_filename = '%s/status' % self._procdir()
168 status_file = open(status_filename, 'r')
169 status = status_file.read().split('\n')
170 status_file.close()
171 for key_value_string in status:
172 colon = key_value_string.find(':')
173 key = key_value_string[:colon].strip()
174 value = key_value_string[colon + 1:].strip()
175 self.__status[key] = value
176 except IOError:
177 exists = False
178 return exists
179
187
189 strings = []
190 try:
191 filename = '%s/%s' % (self._procdir(), filename)
192 file = open(filename, 'r')
193 contents = file.read()
194 file.close()
195 strings = contents.split(chr(0))
196 except IOError:
197 pass
198 return strings
199
201 return '/proc/%s' % self.pid
202