I found there’s a problem with checking GPU’s memory to judge whether the program is running.
When running some “small” program, it’s easy to misjudge.
So I consider judging the number of processes to determine whether the program is over. Because I always use bash to run program, I chose to use number of process instead of process id.
""" Author: Ni Runyu & MonkeyDC Date: 2023-09-14 14:44:30 LastEditors: Ni Runyu & MonkeyDC LastEditTime: 2023-12-14 14:34:30 Description: Check working process number by top Copyright (c) 2023 by Ni Runyu, All Rights Reserved. """
import os import re import time
import yagmail
deftop_process_info(user_name): cpu_info = os.popen(f"top -n 1 -u {user_name}").readlines() processes = list() for line in cpu_info: if'python'in line: processes.append(re.findall(r"\d+", line)[0]) # save the Pid
return processes
if __name__ == "__main__": user_name = os.getlogin() server_name = os.uname()[1]
whileTrue: processes_now = top_process_info(user_name) print(f"On {server_name}: {time.asctime()} : {len(processes_now)} working process") # as using bash, so according number of processes to judge. iflen(processes_now) < processes_num: break_times += 1 else: break_times = 0