clean_gpu.py 630 B

12345678910111213141516171819
  1. import os, re
  2. def clean_gpu():
  3. ret = os.popen("fuser -v /dev/nvidia*").read()
  4. ret = re.sub("kernel", " ", ret)
  5. ids = set(ret.split(" "))
  6. ids = [int(i) for i in ids if i != '']
  7. ids = [str(i) for i in sorted(ids)]
  8. ids_string = ' '.join(ids)
  9. cmd = f"kill -9 {ids_string}"
  10. os.system("fuser -v /dev/nvidia*")
  11. flag = input(f"You are going run this command: \n ==> \"{cmd}\" \nEnter y/Y to proceed, or other to abort.\n[y/n]")
  12. if flag.lower() == 'y':
  13. os.system(cmd)
  14. print("All gpu process cleaned!")
  15. else:
  16. print("Aborted!")
  17. if __name__ == '__main__':
  18. clean_gpu()