Update server10v.py
Browse files- server10v.py +90 -20
server10v.py
CHANGED
|
@@ -108,6 +108,8 @@ from requests.adapters import HTTPAdapter
|
|
| 108 |
from requests.packages.urllib3.util.retry import Retry
|
| 109 |
import logging
|
| 110 |
from datetime import datetime
|
|
|
|
|
|
|
| 111 |
import pytz
|
| 112 |
|
| 113 |
# 创建 FRP 文件夹并下载 frpc 可执行文件。
|
|
@@ -183,6 +185,7 @@ def compress_folder():
|
|
| 183 |
try:
|
| 184 |
# 执行压缩
|
| 185 |
result = subprocess.run(
|
|
|
|
| 186 |
f'tar -cvf - {data_folder} | pigz -p 2 -1 > {zip_file_path} 2> /dev/null',
|
| 187 |
shell=True,
|
| 188 |
check=True,
|
|
@@ -247,6 +250,91 @@ def requests_retry_session(retries=3, backoff_factor=0.3, status_forcelist=(500,
|
|
| 247 |
session.mount('https://', adapter)
|
| 248 |
return session
|
| 249 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
|
| 252 |
def check_api1():
|
|
@@ -306,29 +394,13 @@ def monitor_port():
|
|
| 306 |
config.read('/data/config.ini')
|
| 307 |
monitor_port_enabled = config.getboolean('general', 'monitor_port_enabled', fallback=False)
|
| 308 |
if monitor_port_enabled:
|
|
|
|
| 309 |
backup_thread = threading.Thread(target=Run_SillyTavern, daemon=True)
|
| 310 |
backup_thread.start()
|
| 311 |
else:
|
| 312 |
print(f"monitor_port_enabled 为False跳过")
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
| 316 |
-
# result = sock.connect_ex(('localhost', 8000))
|
| 317 |
-
# if result == 0:
|
| 318 |
-
# print(f"端口 8000 正在使用中")
|
| 319 |
-
# else:
|
| 320 |
-
# print(f"端口 8000 未被使用")
|
| 321 |
-
# # 读取 config.ini 中的 monitor_port_enabled 配置项
|
| 322 |
-
# config = configparser.ConfigParser()
|
| 323 |
-
# config.read('/data/config.ini')
|
| 324 |
-
# monitor_port_enabled = config.getboolean('general', 'monitor_port_enabled', fallback=False)
|
| 325 |
-
# if monitor_port_enabled:
|
| 326 |
-
# backup_thread = threading.Thread(target=Run_SillyTavern, daemon=True)
|
| 327 |
-
# backup_thread.start()
|
| 328 |
-
# else:
|
| 329 |
-
# print(f"monitor_port_enabled 为False跳过")
|
| 330 |
|
| 331 |
-
time.sleep(
|
| 332 |
def Run_SillyTavern():
|
| 333 |
try:
|
| 334 |
os.chdir('/data/SillyTavern')
|
|
@@ -350,8 +422,6 @@ def Run_SillyTavern():
|
|
| 350 |
def SillyTavern():
|
| 351 |
print('-----------SillyTavern开始-------------')
|
| 352 |
# 创建目录并写入 kaggle.json
|
| 353 |
-
|
| 354 |
-
|
| 355 |
# 打印当前工作目录和用户主目录
|
| 356 |
print("Current Working Directory:", os.getcwd())
|
| 357 |
print("Home Directory:", os.path.expanduser('~'))
|
|
|
|
| 108 |
from requests.packages.urllib3.util.retry import Retry
|
| 109 |
import logging
|
| 110 |
from datetime import datetime
|
| 111 |
+
import psutil
|
| 112 |
+
from typing import List, Dict
|
| 113 |
import pytz
|
| 114 |
|
| 115 |
# 创建 FRP 文件夹并下载 frpc 可执行文件。
|
|
|
|
| 185 |
try:
|
| 186 |
# 执行压缩
|
| 187 |
result = subprocess.run(
|
| 188 |
+
# tar -cvf - /data/SillyTavern | pigz -p 2 -1 > /dev/shm/a/sillytavern.tar.gz 2> /dev/null
|
| 189 |
f'tar -cvf - {data_folder} | pigz -p 2 -1 > {zip_file_path} 2> /dev/null',
|
| 190 |
shell=True,
|
| 191 |
check=True,
|
|
|
|
| 250 |
session.mount('https://', adapter)
|
| 251 |
return session
|
| 252 |
|
| 253 |
+
def kill_processes_on_port(port: int = 8000) -> Dict[str, List[int]]:
|
| 254 |
+
"""
|
| 255 |
+
结束指定端口的所有相关进程
|
| 256 |
+
|
| 257 |
+
Args:
|
| 258 |
+
port (int): 要处理的端口号,默认为8000
|
| 259 |
+
|
| 260 |
+
Returns:
|
| 261 |
+
Dict[str, List[int]]: 结束的进程信息
|
| 262 |
+
"""
|
| 263 |
+
killed_processes = {
|
| 264 |
+
'success': [],
|
| 265 |
+
'failed': []
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
try:
|
| 269 |
+
# 遍历所有进程
|
| 270 |
+
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
|
| 271 |
+
try:
|
| 272 |
+
# 检查进程的连接
|
| 273 |
+
connections = proc.net_connections()
|
| 274 |
+
|
| 275 |
+
# 检查是否有指定端口的连接
|
| 276 |
+
port_matches = [
|
| 277 |
+
conn for conn in connections
|
| 278 |
+
if conn.laddr.port == port
|
| 279 |
+
]
|
| 280 |
+
|
| 281 |
+
if port_matches:
|
| 282 |
+
pid = proc.pid
|
| 283 |
+
name = proc.name()
|
| 284 |
+
|
| 285 |
+
print(f"找到端口 {port} 相关进程: PID {pid}, 名称 {name}")
|
| 286 |
+
|
| 287 |
+
try:
|
| 288 |
+
# 尝试优雅地终止进程
|
| 289 |
+
parent = psutil.Process(pid)
|
| 290 |
+
|
| 291 |
+
# 终止进程及其所有子进程
|
| 292 |
+
for child_proc in parent.children(recursive=True):
|
| 293 |
+
try:
|
| 294 |
+
child_proc.terminate()
|
| 295 |
+
print(f"终止子进程: PID {child_proc.pid}")
|
| 296 |
+
except Exception as child_err:
|
| 297 |
+
print(f"无法终止子进程 {child_proc.pid}: {child_err}")
|
| 298 |
+
|
| 299 |
+
parent.terminate()
|
| 300 |
+
|
| 301 |
+
# 等待进程结束
|
| 302 |
+
gone, alive = psutil.wait_procs([parent], timeout=3)
|
| 303 |
+
|
| 304 |
+
for p in alive:
|
| 305 |
+
print(f"强制杀死进程: PID {p.pid}")
|
| 306 |
+
p.kill()
|
| 307 |
+
|
| 308 |
+
killed_processes['success'].append(pid)
|
| 309 |
+
print(f"成功终止进程 {pid}")
|
| 310 |
+
|
| 311 |
+
except psutil.NoSuchProcess:
|
| 312 |
+
print(f"进程 {pid} 已不存在")
|
| 313 |
+
except psutil.AccessDenied:
|
| 314 |
+
print(f"没有权限终止进程 {pid}")
|
| 315 |
+
killed_processes['failed'].append(pid)
|
| 316 |
+
except Exception as e:
|
| 317 |
+
print(f"终止进程 {pid} 时发生错误: {e}")
|
| 318 |
+
killed_processes['failed'].append(pid)
|
| 319 |
+
|
| 320 |
+
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
| 321 |
+
continue
|
| 322 |
+
|
| 323 |
+
except Exception as e:
|
| 324 |
+
print(f"搜索进程时发生错误: {e}")
|
| 325 |
+
|
| 326 |
+
# 打印总结报告
|
| 327 |
+
print("--- 进程终止报告 ---")
|
| 328 |
+
print(f"成功终止进程数: {len(killed_processes['success'])}")
|
| 329 |
+
print(f"终止失败进程数: {len(killed_processes['failed'])}")
|
| 330 |
+
|
| 331 |
+
if killed_processes['success']:
|
| 332 |
+
print(f"成功终止的进程 PID: {killed_processes['success']}")
|
| 333 |
+
|
| 334 |
+
if killed_processes['failed']:
|
| 335 |
+
print(f"终止失败的进程 PID: {killed_processes['failed']}")
|
| 336 |
+
|
| 337 |
+
return killed_processes
|
| 338 |
|
| 339 |
|
| 340 |
def check_api1():
|
|
|
|
| 394 |
config.read('/data/config.ini')
|
| 395 |
monitor_port_enabled = config.getboolean('general', 'monitor_port_enabled', fallback=False)
|
| 396 |
if monitor_port_enabled:
|
| 397 |
+
kill_processes_on_port()
|
| 398 |
backup_thread = threading.Thread(target=Run_SillyTavern, daemon=True)
|
| 399 |
backup_thread.start()
|
| 400 |
else:
|
| 401 |
print(f"monitor_port_enabled 为False跳过")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
+
time.sleep(15) # 每5秒检查一次
|
| 404 |
def Run_SillyTavern():
|
| 405 |
try:
|
| 406 |
os.chdir('/data/SillyTavern')
|
|
|
|
| 422 |
def SillyTavern():
|
| 423 |
print('-----------SillyTavern开始-------------')
|
| 424 |
# 创建目录并写入 kaggle.json
|
|
|
|
|
|
|
| 425 |
# 打印当前工作目录和用户主目录
|
| 426 |
print("Current Working Directory:", os.getcwd())
|
| 427 |
print("Home Directory:", os.path.expanduser('~'))
|