#!/usr/bin/env python from os import popen, system from string import strip, split programname = 'mpifxcorr' verbose = 1 killfirst = False def getpids(): cmd = 'ps -e | grep %s' % programname ps = popen(cmd, 'r') pslines = ps.readlines() ps.close() pids = [] for p in pslines: s = split(strip(p)) if len(s) < 2: continue if s[3] != programname: continue pids.append(int(s[0])) return pids pids = getpids() if verbose > 0: print 'PIDs = ', pids if len(pids) == 0: print 'no %s process appears to be running' % programname elif len(pids) == 1: cmd = 'kill -INT %d' % (pids[0]) print 'killing a process with: ', cmd system(cmd) elif killfirst: cmd = 'kill -INT %d' % (pids[0]) print 'killing first matching process with: ', cmd system(cmd) else: print 'too many processes called %s running. not killing' % \ programname print 'kill by hand with "kill -INT " where is one of ', \ pids