针对后端常驻内存的程序 : 检查是否有对应进程 没有的话进行启动
import psutil,pdb import subprocess def get_process_id(process_name): return def check_and_start_process(process_name, start_command): ss=psutil.process_iter() for proc in psutil.process_iter(): # pdb.set_trace() # print(proc.pid,proc.cmdline()) if process_name in proc.cmdline(): print(f"Process '{process_name}' is already running.") return print(f"Process '{process_name}' is not running. Starting it...") subprocess.Popen(start_command, shell=True) # 要检测的进程名称 process_name = '*****.py' # 启动进程的命令 start_command = 'nohup **.py >/dev/null 2>&1 &' check_and_start_process(process_name, start_command)