r"^ *(\d+) .*/led-image-viewer" def kill_img (): id = [] ps_list = commands.getoutput('ps awx') for process in ps_list.split("\n"): match = re.match(pattern, process) if not match: continue id.append(match.group(1)) commands.getoutput("sudo kill "+" ".join(id))