53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
import time
|
|
import psutil
|
|
import subprocess
|
|
import telegram
|
|
import asyncio
|
|
from config import *
|
|
|
|
class ProcessMonitor:
|
|
|
|
def __init__(self, python_executable="python"):
|
|
self.python = python_executable
|
|
# 실행된 프로세스 저장용
|
|
self.process_map = {}
|
|
|
|
def is_running(self, script_path):
|
|
"""해당 스크립트가 실행 중인지 확인"""
|
|
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
|
|
try:
|
|
if proc.info['cmdline'] and script_path in proc.info['cmdline']:
|
|
return True
|
|
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
|
continue
|
|
return False
|
|
|
|
def start_process(self, script_path):
|
|
"""해당 스크립트 실행"""
|
|
print(f"[INFO] Starting {script_path}")
|
|
process = subprocess.Popen([self.python, script_path], creationflags=subprocess.CREATE_NEW_CONSOLE)
|
|
self.process_map[script_path] = process
|
|
|
|
def sendMsg(self, text):
|
|
coin_client = telegram.Bot(token=COIN_TELEGRAM_BOT_TOKEN)
|
|
asyncio.run(coin_client.send_message(chat_id=COIN_TELEGRAM_CHAT_ID, text=text))
|
|
return
|
|
|
|
def monitor(self, scripts, interval=60):
|
|
"""1분 단위로 프로세스 상태 확인 및 관리"""
|
|
while True:
|
|
for script in scripts:
|
|
if not self.is_running(script):
|
|
self.sendMsg("🔔{} process is killed.".format(script))
|
|
|
|
time.sleep(interval)
|
|
|
|
if __name__ == "__main__":
|
|
monitor = ProcessMonitor()
|
|
|
|
# 모니터링할 스크립트 목록
|
|
scripts = [
|
|
r"C:\workspace\AssetMonitor\monitor_coin_1h_1.py",
|
|
r"C:\workspace\AssetMonitor\monitor_coin_1h_2.py"
|
|
]
|
|
monitor.monitor(scripts, interval=60) |