Files

30 lines
1.1 KiB
Python
Raw Permalink Normal View History

import subprocess, os
pid_file = r'C:\Users\whdwo\OneDrive\바탕 화면\stockbot_v3\logs\bot.pid'
# 1-a) PID 파일로 종료
if os.path.exists(pid_file):
try:
pid = int(open(pid_file).read().strip())
subprocess.run(['taskkill', '/PID', str(pid), '/F'], capture_output=True)
print(f'PID 파일 종료: {pid}')
except Exception as e:
print(f'PID 파일 종료 실패: {e}')
os.remove(pid_file)
else:
print('PID 파일 없음')
# 1-b) Get-CimInstance로 잔존 프로세스 스캔
r = subprocess.run(
['powershell', '-Command',
'Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -like "*app/main.py*" -or $_.CommandLine -like "*app\\main.py*" } | Select-Object -ExpandProperty ProcessId'],
capture_output=True, text=True
)
pids = [p.strip() for p in r.stdout.strip().splitlines() if p.strip().isdigit()]
for pid in pids:
subprocess.run(['taskkill', '/PID', pid, '/F'], capture_output=True)
print(f'잔존 프로세스 종료: {pid}')
if not pids:
print('실행 중인 봇 없음 — 새로 시작합니다')