@task
def kill(signal=9):
	""" Kill zombie (unregistered) modules """
	zombies = []
	pids = set(m[1] for m in running_modules())

	for line in shell('ps -e --no-headers -o pid,command').split('\n'):
		try: pid, name = re.match(r'\s*(\d+)\s+(.+)$', line).groups()
		except AttributeError: continue

		mob = re.search(r'app/(.*?)/main', name)
		if mob and pid not in pids:
			zombies.append((mob.group(1), pid))

	if not zombies:
		puts(fore.green('No zombies detected'))
	else:
		puts(fore.green('Killing %d zombie modules' % len(zombies)))
		for name, pid in zombies:
			puts(' * Killing %-10s (pid: %s) ...' % (fore.cyan(name), fore.red(pid)))

			try: shell('kill -s %s %s' % (signal, pid), exception=RuntimeError)
			except RuntimeError as e:
				puts(fore.magenta('   Error: ') + e.args[0])
dataset/ETHPy150Open alexcepoi/pyscale/pyscale/tools/tasks.py/kill/original