-
Notifications
You must be signed in to change notification settings - Fork 4
/
auto_monitor.py
56 lines (50 loc) · 1.96 KB
/
auto_monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import psutil
import subprocess
import time
import os
def is_script_running(script_name):
for proc in psutil.process_iter(['name', 'cmdline']):
try:
if proc.info['name'] == 'python' or proc.info['name'] == 'python.exe':
cmdline = proc.info['cmdline']
if cmdline and script_name in ' '.join(cmdline):
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
def kill_script(script_name):
for proc in psutil.process_iter(['name', 'cmdline']):
try:
if proc.info['name'] == 'python' or proc.info['name'] == 'python.exe':
cmdline = proc.info['cmdline']
if cmdline and script_name in ' '.join(cmdline):
proc.kill()
print(f"Killed {script_name}")
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
def start_script(script_path):
try:
subprocess.Popen(['python', script_path],
cwd=os.path.dirname(script_path))
print(f"Started {script_path}")
except Exception as e:
print(f"Error starting script: {e}")
def main():
script_path = r"/home/yubo/VillagerAgent-Minecraft-multiagent-framework/auto_gen_gpt_task.py"
script_name = os.path.basename(script_path)
while True:
try:
if not is_script_running(script_name):
print(f"[Monitor]: [{script_name} is not running. Attempting to start...]")
start_script(script_path)
else:
print(f"[Monitor]: [{script_name} is already running.]")
except KeyboardInterrupt:
print("[Monitor]: killed the running script.")
kill_script(script_name)
break
# Wait for 30s before checking again
time.sleep(30)
if __name__ == "__main__":
main()
# pkill -f "python|node.*bridge.js"