# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import os
-import psutil
+import errno
+import sys
+try:
+ import psutil
+except OSError, e:
+ # XXX: This is a hack, but since we are run from cron, it's
+ # better to handle this on the next run than to send mail.
+ if e.errno == errno.ENOENT:
+ sys.exit(0)
-total_mem = psutil.phymem_usage().total
+total_mem = psutil.virtual_memory().total
cutoff_time = 60*10
for p in psutil.process_iter():
- if p.name != 'aptitude': continue
- parent = p.parent
- if parent is None: continue
- if parent.name != 'schroot': continue
- #
- rootdir = os.readlink('/proc/%d/root'%(p.pid,))
- if not rootdir.startswith('/var/lib/schroot/mount'): continue
- #
- used = p.get_memory_info().vms
- if used < total_mem: continue
- #
- cputime = p.get_cpu_times().user
- if cputime < cutoff_time: continue
- #
- p.kill()
+ try:
+ if p.name() != 'aptitude': continue
+ parent = p.parent()
+ if parent is None: continue
+ if parent.name() != 'schroot': continue
+ #
+ try:
+ rootdir = os.readlink('/proc/%d/root'%(p.pid,))
+ except OSError as e:
+ if e.errno == errno.ENOENT:
+ continue
+ else:
+ raise e
+ if not rootdir.startswith('/var/lib/schroot/mount'): continue
+ #
+ used = p.get_memory_info().vms
+ cputime = p.get_cpu_times().user
+ if used < total_mem and cputime < cutoff_time: continue
+ #
+ p.kill()
+ except psutil.NoSuchProcess:
+ pass