import os, signal def kill_process(process_name): try: for line in os.popen('ps ax | grep ' + process_name + ' | grep -v grep'): fields = line.split() if fields[4] == process_name: pid = fields[0] os.kill(int(pid), signal.SIGKILL) return True except Exception as e: print(f'{e}') return False kill_process('sleep')