Update server10.py
Browse files- server10.py +176 -125
server10.py
CHANGED
|
@@ -4,9 +4,12 @@
|
|
| 4 |
#lsof -i :8000
|
| 5 |
# 查找server.py进程
|
| 6 |
#ps aux | grep server.py
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
ini_content = {"username":"tmpysanneernier91","key":"fcffd91fcc8a94fb4a8d3a1b39ff652c"}
|
| 12 |
frp_url = '119.8.118.149'
|
|
@@ -29,58 +32,7 @@ configs = [
|
|
| 29 |
local_ip = 127.0.0.1
|
| 30 |
local_port = 8000
|
| 31 |
remote_port = 7091
|
| 32 |
-
""", '/data/frp/frpc.ini')
|
| 33 |
-
(f"""
|
| 34 |
-
[common]
|
| 35 |
-
server_addr = {frp_url}
|
| 36 |
-
server_port = 7000
|
| 37 |
-
token = qilan
|
| 38 |
-
[1kkjmk1010]
|
| 39 |
-
type = tcp
|
| 40 |
-
local_ip = 127.0.0.1
|
| 41 |
-
local_port = 8000
|
| 42 |
-
remote_port = 7090
|
| 43 |
-
""", '/data/frp/frpc1.ini'),
|
| 44 |
-
("""
|
| 45 |
-
[common]
|
| 46 |
-
server_addr = 43.134.75.213
|
| 47 |
-
server_port = 7000
|
| 48 |
-
[2kjas9]
|
| 49 |
-
type = tcp
|
| 50 |
-
local_ip = 127.0.0.1
|
| 51 |
-
local_port = 8000
|
| 52 |
-
remote_port = 7079
|
| 53 |
-
""", '/data/frp/frpc2.ini'),
|
| 54 |
-
("""
|
| 55 |
-
[common]
|
| 56 |
-
server_addr = 8.210.217.27
|
| 57 |
-
server_port = 7000
|
| 58 |
-
[3kja1sh9]
|
| 59 |
-
type = tcp
|
| 60 |
-
local_ip = 127.0.0.1
|
| 61 |
-
local_port = 8000
|
| 62 |
-
remote_port = 7079
|
| 63 |
-
""", '/data/frp/frpc3.ini'),
|
| 64 |
-
("""
|
| 65 |
-
[common]
|
| 66 |
-
server_addr = 45.32.68.209
|
| 67 |
-
server_port=7000
|
| 68 |
-
[4kkji1mk9]
|
| 69 |
-
type=tcp
|
| 70 |
-
local_ip=127.0.0.1
|
| 71 |
-
local_port=8000
|
| 72 |
-
remote_port=7079
|
| 73 |
-
""", '/data/frp/frpc4.ini'),
|
| 74 |
-
(f"""
|
| 75 |
-
[common]
|
| 76 |
-
server_addr = {ssh_url}
|
| 77 |
-
server_port=7000
|
| 78 |
-
[5kkji1mk99]
|
| 79 |
-
type=tcp
|
| 80 |
-
local_ip=127.0.0.1
|
| 81 |
-
local_port=22
|
| 82 |
-
remote_port={ssh_port}
|
| 83 |
-
""", '/data/frp/frpc5.ini')
|
| 84 |
]
|
| 85 |
kaggle_metadata = {
|
| 86 |
"title": "Your Dataset Title",
|
|
@@ -108,19 +60,35 @@ 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 |
-
os.system("wget -O /sync.sh https://huggingface.co/datasets/Qilan2/st-server/resolve/main/sync.sh" )
|
| 114 |
-
os.system("export LC_ALL=zh_CN.UTF-8")
|
| 115 |
# 创建 FRP 文件夹并下载 frpc 可执行文件。
|
| 116 |
os.system("mkdir -p /data/frp")
|
| 117 |
-
os.system("wget /data/frp/frpc https://huggingface.co/Qilan2/box/resolve/main/frp/frpc -O /data/frp/frpc")
|
| 118 |
os.system("chmod +x /data/frp/frpc")
|
| 119 |
-
os.system("wget https://huggingface.co/datasets/Qilan2/st-server/resolve/main/xugou-agent-linux-amd64?download=true -O /data/xugou-agent")
|
| 120 |
os.system("chmod +x /data/xugou-agent")
|
| 121 |
os.system('nohup /data/xugou-agent start --server "https://tz.282820.xyz" --token "xugou_meqz1qus_80244ac171f1d9cf3a1ead6be2ecf86f.7d4ae786cbb7e559" --interval "30" > /data/xugou-agent.log 2>&1 &')
|
| 122 |
-
os.system("wget https://huggingface.co/datasets/Qilan2/st-server/raw/main/ip_huggingface.py -O /data/ip_huggingface.py")
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
def kill_frpc_processes():
|
| 125 |
try:
|
| 126 |
# 使用pkill命令结束所有frpc进程
|
|
@@ -132,9 +100,6 @@ def kill_frpc_processes():
|
|
| 132 |
kill_frpc_processes()
|
| 133 |
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
# 创建一���锁
|
| 139 |
lock = threading.Lock()
|
| 140 |
|
|
@@ -185,6 +150,7 @@ def compress_folder():
|
|
| 185 |
try:
|
| 186 |
# 执行压缩
|
| 187 |
result = subprocess.run(
|
|
|
|
| 188 |
f'tar -cvf - {data_folder} | pigz -p 2 -1 > {zip_file_path} 2> /dev/null',
|
| 189 |
shell=True,
|
| 190 |
check=True,
|
|
@@ -235,9 +201,11 @@ def repeat_task():
|
|
| 235 |
print('------打包线程启动-------------')
|
| 236 |
while True:
|
| 237 |
print('等待打包')
|
| 238 |
-
time.sleep(
|
|
|
|
| 239 |
compress_folder()
|
| 240 |
-
|
|
|
|
| 241 |
|
| 242 |
|
| 243 |
def requests_retry_session(retries=3, backoff_factor=0.3, status_forcelist=(500, 502, 504), session=None):
|
|
@@ -249,6 +217,91 @@ def requests_retry_session(retries=3, backoff_factor=0.3, status_forcelist=(500,
|
|
| 249 |
session.mount('https://', adapter)
|
| 250 |
return session
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
|
| 254 |
def check_api1():
|
|
@@ -298,48 +351,35 @@ def monitor_port():
|
|
| 298 |
while True:
|
| 299 |
url = "http://127.0.0.1:8000/v1"
|
| 300 |
response = requests.get(url, timeout=30)
|
| 301 |
-
if response.status_code =
|
| 302 |
-
print("SillyTavern运行中")
|
| 303 |
-
# break
|
| 304 |
-
else:
|
| 305 |
print("SillyTavern未运行,状态码:", response.status_code, n)
|
| 306 |
# 读取 config.ini 中的 monitor_port_enabled 配置项
|
| 307 |
config = configparser.ConfigParser()
|
| 308 |
config.read('/data/config.ini')
|
| 309 |
monitor_port_enabled = config.getboolean('general', 'monitor_port_enabled', fallback=False)
|
| 310 |
if monitor_port_enabled:
|
|
|
|
| 311 |
backup_thread = threading.Thread(target=Run_SillyTavern, daemon=True)
|
| 312 |
backup_thread.start()
|
| 313 |
else:
|
| 314 |
print(f"monitor_port_enabled 为False跳过")
|
| 315 |
-
|
| 316 |
|
| 317 |
-
|
| 318 |
-
# result = sock.connect_ex(('localhost', 8000))
|
| 319 |
-
# if result == 0:
|
| 320 |
-
# print(f"端口 8000 正在使用中")
|
| 321 |
-
# else:
|
| 322 |
-
# print(f"端口 8000 未被使用")
|
| 323 |
-
# # 读取 config.ini 中的 monitor_port_enabled 配置项
|
| 324 |
-
# config = configparser.ConfigParser()
|
| 325 |
-
# config.read('/data/config.ini')
|
| 326 |
-
# monitor_port_enabled = config.getboolean('general', 'monitor_port_enabled', fallback=False)
|
| 327 |
-
# if monitor_port_enabled:
|
| 328 |
-
# backup_thread = threading.Thread(target=Run_SillyTavern, daemon=True)
|
| 329 |
-
# backup_thread.start()
|
| 330 |
-
# else:
|
| 331 |
-
# print(f"monitor_port_enabled 为False跳过")
|
| 332 |
-
|
| 333 |
-
time.sleep(5) # 每5秒检查一次
|
| 334 |
def Run_SillyTavern():
|
| 335 |
-
os.chdir(data_folder)
|
| 336 |
try:
|
|
|
|
| 337 |
# 启动 Node.js 服务器,并将输出重定向到 /dev/null
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
print("已尝试启动 server.js")
|
| 345 |
except subprocess.CalledProcessError as e:
|
|
@@ -350,8 +390,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('~'))
|
|
@@ -381,8 +419,11 @@ def SillyTavern():
|
|
| 381 |
|
| 382 |
os.system(f"rm -rf /a /data/sillytavern /data/data /data/*.zip /data/*.ctk")
|
| 383 |
# os.system(f"mkdir -p {data_folder}")
|
|
|
|
|
|
|
|
|
|
| 384 |
os.chdir('/data')
|
| 385 |
-
|
| 386 |
print(f"kaggle datasets download {huggingface_name}/{huggingface_Data_name}")
|
| 387 |
# os.system(f"kaggle datasets download {huggingface_name}/{huggingface_Data_name}")
|
| 388 |
download_n = 0
|
|
@@ -404,36 +445,46 @@ def SillyTavern():
|
|
| 404 |
print(f"文件 /{huggingface_Data_name}.zip 不存在,启用实时同步。")
|
| 405 |
|
| 406 |
else:
|
| 407 |
-
|
| 408 |
# 如果文件存在,可以继续执行后续代码
|
|
|
|
|
|
|
| 409 |
print(f"文件 /data/{huggingface_Data_name}.zip 存在,继续执行程序。")
|
| 410 |
# # 解压 sillytavern.zip
|
| 411 |
# os.system("rm *.zip")
|
| 412 |
os.system(f"7z x /data/{huggingface_Data_name}.zip -y")
|
| 413 |
-
os.system("mv /data/sillytavern.tck sillytavern.tar.gz")
|
| 414 |
os.system("mv /data/sillytavern.ctk sillytavern.tar.gz")
|
| 415 |
-
os.system("ls /data")
|
| 416 |
-
# os.system("tar -xvpf /sillytavern.tar.gz -C / --use-compress-program=pigz")
|
| 417 |
-
|
|
|
|
| 418 |
# os.system("pv /sillytavern.tar.gz | tar -xvpf - -C / --use-compress-program=pigz")
|
| 419 |
-
os.system("mv /data/data/SillyTavern /data/SillyTavern")
|
| 420 |
-
|
| 421 |
-
print('更新
|
|
|
|
| 422 |
os.chdir('/data/SillyTavern')
|
|
|
|
|
|
|
| 423 |
os.system(f"ls {data_folder}")
|
|
|
|
| 424 |
os.system("nvm install node")
|
| 425 |
os.system("nvm use node")
|
| 426 |
os.system("npm install -y")
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
os.system("
|
|
|
|
|
|
|
| 434 |
backup_thread = threading.Thread(target=Run_SillyTavern, daemon=True)
|
| 435 |
backup_thread.start()
|
| 436 |
# os.system(f"rm -rf /a /data/sillytavern /data/data /data/*.zip /data/*.ctk")
|
|
|
|
| 437 |
|
| 438 |
print('-----------SillyTavern结束-------------')
|
| 439 |
import requests
|
|
@@ -484,6 +535,8 @@ def restart_huggingface_space(space_name, space_id, partial_token):
|
|
| 484 |
import psutil
|
| 485 |
|
| 486 |
def check_system_resources():
|
|
|
|
|
|
|
| 487 |
# 获取CPU使用率
|
| 488 |
cpu_usage = psutil.cpu_percent(interval=1)
|
| 489 |
|
|
@@ -492,14 +545,15 @@ def check_system_resources():
|
|
| 492 |
memory_usage = memory.percent
|
| 493 |
|
| 494 |
# 检查CPU和内存占用是否超过85%
|
| 495 |
-
if cpu_usage >= 90
|
|
|
|
| 496 |
print("占用过高")
|
| 497 |
# 可选:打印具体的使用率
|
| 498 |
print(f"CPU使用率: {cpu_usage}%")
|
| 499 |
print(f"内存使用率: {memory_usage}%")
|
| 500 |
result = restart_huggingface_space(huggingface_user, huggingface_id, huggingface_tokon)
|
| 501 |
print(result)
|
| 502 |
-
time.sleep(6666666)
|
| 503 |
else:
|
| 504 |
print("系统资源正常")
|
| 505 |
|
|
@@ -511,7 +565,7 @@ threading.Thread(target=SillyTavern, daemon=True).start()
|
|
| 511 |
# SillyTavern()
|
| 512 |
time.sleep(60)
|
| 513 |
check_api1()
|
| 514 |
-
|
| 515 |
|
| 516 |
# 创建 ConfigParser 对象
|
| 517 |
config = configparser.ConfigParser()
|
|
@@ -530,21 +584,19 @@ if os.path.exists(f'/data/{huggingface_Data_name}.zip'):
|
|
| 530 |
Service_status = True
|
| 531 |
frp(configs, True)
|
| 532 |
threading.Thread(target=monitor_port, daemon=True).start()#检测端口状态
|
| 533 |
-
|
| 534 |
# time.sleep(600)
|
| 535 |
while True:
|
| 536 |
# 检查是否是中国时区早上6点
|
| 537 |
china_tz = pytz.timezone('Asia/Shanghai')
|
| 538 |
now = datetime.now(china_tz)
|
| 539 |
-
if now.hour ==
|
| 540 |
print(f"当前时间: {now.strftime('%Y-%m-%d %H:%M:%S')}, 到达早上6点,退出循环")
|
| 541 |
break
|
| 542 |
-
else:
|
| 543 |
-
|
| 544 |
|
| 545 |
-
if backup_thread.is_alive():
|
| 546 |
-
print("备份线程仍在运行...")
|
| 547 |
-
else:
|
| 548 |
print("备份线程已停止.")
|
| 549 |
backup_thread = threading.Thread(target=repeat_task, daemon=True)
|
| 550 |
backup_thread.start()
|
|
@@ -553,5 +605,4 @@ else:
|
|
| 553 |
print(f'{data_folder}/start.sh 文件不存在结束执行')
|
| 554 |
compress_folder()
|
| 555 |
result = restart_huggingface_space(huggingface_user, huggingface_id, huggingface_tokon)
|
| 556 |
-
print(result)
|
| 557 |
-
|
|
|
|
| 4 |
#lsof -i :8000
|
| 5 |
# 查找server.py进程
|
| 6 |
#ps aux | grep server.py
|
| 7 |
+
#source /opt/venv/bin/activate
|
| 8 |
+
#python a.py
|
| 9 |
+
|
| 10 |
+
huggingface_user="sahdbxuuu"#
|
| 11 |
+
huggingface_id="s10"
|
| 12 |
+
huggingface_tokon="hf_ImrWBdTCO xnBCNtFcVpITLmrMJmHgthmtS"
|
| 13 |
|
| 14 |
ini_content = {"username":"tmpysanneernier91","key":"fcffd91fcc8a94fb4a8d3a1b39ff652c"}
|
| 15 |
frp_url = '119.8.118.149'
|
|
|
|
| 32 |
local_ip = 127.0.0.1
|
| 33 |
local_port = 8000
|
| 34 |
remote_port = 7091
|
| 35 |
+
""", '/data/frp/frpc.ini')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
]
|
| 37 |
kaggle_metadata = {
|
| 38 |
"title": "Your Dataset Title",
|
|
|
|
| 60 |
from requests.packages.urllib3.util.retry import Retry
|
| 61 |
import logging
|
| 62 |
from datetime import datetime
|
| 63 |
+
import psutil
|
| 64 |
+
from typing import List, Dict
|
| 65 |
import pytz
|
| 66 |
+
|
|
|
|
|
|
|
| 67 |
# 创建 FRP 文件夹并下载 frpc 可执行文件。
|
| 68 |
os.system("mkdir -p /data/frp")
|
| 69 |
+
os.system("wget /data/frp/frpc -q https://huggingface.co/Qilan2/box/resolve/main/frp/frpc -O /data/frp/frpc")
|
| 70 |
os.system("chmod +x /data/frp/frpc")
|
| 71 |
+
os.system("wget -q https://huggingface.co/datasets/Qilan2/st-server/resolve/main/xugou-agent-linux-amd64?download=true -O /data/xugou-agent")
|
| 72 |
os.system("chmod +x /data/xugou-agent")
|
| 73 |
os.system('nohup /data/xugou-agent start --server "https://tz.282820.xyz" --token "xugou_meqz1qus_80244ac171f1d9cf3a1ead6be2ecf86f.7d4ae786cbb7e559" --interval "30" > /data/xugou-agent.log 2>&1 &')
|
| 74 |
+
os.system("wget -q https://huggingface.co/datasets/Qilan2/st-server/raw/main/ip_huggingface.py -O /data/ip_huggingface.py")
|
| 75 |
+
# os.system("wget -O '/data/nezha-agent-amd64' 'https://huggingface.co/datasets/Qilan2/st-server/resolve/main/nezha-agent-v0.17.6?download=true'")
|
| 76 |
+
# os.system("chmod +x /data/nezha-agent-amd64")
|
| 77 |
+
# os.system('nohup /data/nezha-agent-amd64 -s tzz.282820.xyz:443 -p aLLlygYqAXgjq9Anln --tls --debug --temperature --disable-force-update --disable-auto-update > nezha-agent-amd64.log 2>&1 &')
|
| 78 |
+
# os.system('/data/nezha-agent-amd64 -s tzz.282820.xyz:443 -p aLLlygYqAXgjq9Anln --tls --debug --temperature --disable-force-update --disable-auto-update')
|
| 79 |
+
|
| 80 |
+
def nezha():
|
| 81 |
+
os.system("rm -r /data/nezha-agent-amd64")
|
| 82 |
+
os.system("wget -O '/data/nezha-agent-amd64' 'https://huggingface.co/datasets/Qilan2/st-server/resolve/main/nezha-agent-v0.17.6?download=true'")
|
| 83 |
+
os.system("chmod +x /data/nezha-agent-amd64")
|
| 84 |
+
os.system('/data/nezha-agent-amd64 -s tzz.282820.xyz:443 -p aLLlygYqAXgjq9Anln --tls --debug --temperature --disable-force-update --disable-auto-update')
|
| 85 |
+
def cloudflared():
|
| 86 |
+
os.system("rm -r /data/cloudflared-linux-amd64")
|
| 87 |
+
os.system("wget -O '/data/cloudflared-linux-amd64' -q 'https://github.com/cloudflare/cloudflared/releases/download/2025.9.0/cloudflared-linux-amd64'")
|
| 88 |
+
os.system("chmod +x /data/cloudflared-linux-amd64")
|
| 89 |
+
os.system('/data/cloudflared-linux-amd64 tunnel run --token eyJhIjoiNmVjNWE5NzEzOGIzMTg4YTU2Y2U1NjdmMWRhZDBhMTUiLCJ0IjoiMzg0OWFhNjgtMDJhMS00YWYyLWEzMTMtYzQ3ZmRiYTRjNzE3IiwicyI6IlpUTTRZemRsT1RFdE5tVXhOUzAwTlRVeUxUbGhZbUV0TmpFM09UWm1NR1ExTkdJMCJ9')
|
| 90 |
+
# threading.Thread(target=nezha, daemon=True).start()
|
| 91 |
+
# threading.Thread(target=cloudflared, daemon=True).start()
|
| 92 |
def kill_frpc_processes():
|
| 93 |
try:
|
| 94 |
# 使用pkill命令结束所有frpc进程
|
|
|
|
| 100 |
kill_frpc_processes()
|
| 101 |
|
| 102 |
|
|
|
|
|
|
|
|
|
|
| 103 |
# 创建一���锁
|
| 104 |
lock = threading.Lock()
|
| 105 |
|
|
|
|
| 150 |
try:
|
| 151 |
# 执行压缩
|
| 152 |
result = subprocess.run(
|
| 153 |
+
# tar -cvf - /data/SillyTavern | pigz -p 2 -1 > /dev/shm/a/sillytavern.tar.gz 2> /dev/null
|
| 154 |
f'tar -cvf - {data_folder} | pigz -p 2 -1 > {zip_file_path} 2> /dev/null',
|
| 155 |
shell=True,
|
| 156 |
check=True,
|
|
|
|
| 201 |
print('------打包线程启动-------------')
|
| 202 |
while True:
|
| 203 |
print('等待打包')
|
| 204 |
+
# time.sleep(600)
|
| 205 |
+
time.sleep(10800)# 2小时
|
| 206 |
compress_folder()
|
| 207 |
+
|
| 208 |
+
|
| 209 |
|
| 210 |
|
| 211 |
def requests_retry_session(retries=3, backoff_factor=0.3, status_forcelist=(500, 502, 504), session=None):
|
|
|
|
| 217 |
session.mount('https://', adapter)
|
| 218 |
return session
|
| 219 |
|
| 220 |
+
def kill_processes_on_port(port: int = 8000) -> Dict[str, List[int]]:
|
| 221 |
+
"""
|
| 222 |
+
结束指定端口的所有相关进程
|
| 223 |
+
|
| 224 |
+
Args:
|
| 225 |
+
port (int): 要处理的端口号,默认为8000
|
| 226 |
+
|
| 227 |
+
Returns:
|
| 228 |
+
Dict[str, List[int]]: 结束的进程信息
|
| 229 |
+
"""
|
| 230 |
+
killed_processes = {
|
| 231 |
+
'success': [],
|
| 232 |
+
'failed': []
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
try:
|
| 236 |
+
# 遍历所有进程
|
| 237 |
+
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
|
| 238 |
+
try:
|
| 239 |
+
# 检查进程的连接
|
| 240 |
+
connections = proc.net_connections()
|
| 241 |
+
|
| 242 |
+
# 检查是否有指定端口的连接
|
| 243 |
+
port_matches = [
|
| 244 |
+
conn for conn in connections
|
| 245 |
+
if conn.laddr.port == port
|
| 246 |
+
]
|
| 247 |
+
|
| 248 |
+
if port_matches:
|
| 249 |
+
pid = proc.pid
|
| 250 |
+
name = proc.name()
|
| 251 |
+
|
| 252 |
+
print(f"找到端口 {port} 相关进程: PID {pid}, 名称 {name}")
|
| 253 |
+
|
| 254 |
+
try:
|
| 255 |
+
# 尝试优雅地终止进程
|
| 256 |
+
parent = psutil.Process(pid)
|
| 257 |
+
|
| 258 |
+
# 终止进程及其所有子进程
|
| 259 |
+
for child_proc in parent.children(recursive=True):
|
| 260 |
+
try:
|
| 261 |
+
child_proc.terminate()
|
| 262 |
+
print(f"终止子进程: PID {child_proc.pid}")
|
| 263 |
+
except Exception as child_err:
|
| 264 |
+
print(f"无法终止子进程 {child_proc.pid}: {child_err}")
|
| 265 |
+
|
| 266 |
+
parent.terminate()
|
| 267 |
+
|
| 268 |
+
# 等待进程结束
|
| 269 |
+
gone, alive = psutil.wait_procs([parent], timeout=3)
|
| 270 |
+
|
| 271 |
+
for p in alive:
|
| 272 |
+
print(f"强制杀死进程: PID {p.pid}")
|
| 273 |
+
p.kill()
|
| 274 |
+
|
| 275 |
+
killed_processes['success'].append(pid)
|
| 276 |
+
print(f"成功终止进程 {pid}")
|
| 277 |
+
|
| 278 |
+
except psutil.NoSuchProcess:
|
| 279 |
+
print(f"进程 {pid} 已不存在")
|
| 280 |
+
except psutil.AccessDenied:
|
| 281 |
+
print(f"没有权限终止进程 {pid}")
|
| 282 |
+
killed_processes['failed'].append(pid)
|
| 283 |
+
except Exception as e:
|
| 284 |
+
print(f"终止进程 {pid} 时发生错误: {e}")
|
| 285 |
+
killed_processes['failed'].append(pid)
|
| 286 |
+
|
| 287 |
+
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
| 288 |
+
continue
|
| 289 |
+
|
| 290 |
+
except Exception as e:
|
| 291 |
+
print(f"搜索进程时发生错误: {e}")
|
| 292 |
+
|
| 293 |
+
# 打印总结报告
|
| 294 |
+
print("--- 进程终止报告 ---")
|
| 295 |
+
print(f"成功终止进程数: {len(killed_processes['success'])}")
|
| 296 |
+
print(f"终止失败进程数: {len(killed_processes['failed'])}")
|
| 297 |
+
|
| 298 |
+
if killed_processes['success']:
|
| 299 |
+
print(f"成功终止的进程 PID: {killed_processes['success']}")
|
| 300 |
+
|
| 301 |
+
if killed_processes['failed']:
|
| 302 |
+
print(f"终止失败的进程 PID: {killed_processes['failed']}")
|
| 303 |
+
|
| 304 |
+
return killed_processes
|
| 305 |
|
| 306 |
|
| 307 |
def check_api1():
|
|
|
|
| 351 |
while True:
|
| 352 |
url = "http://127.0.0.1:8000/v1"
|
| 353 |
response = requests.get(url, timeout=30)
|
| 354 |
+
if response.status_code != 403:
|
|
|
|
|
|
|
|
|
|
| 355 |
print("SillyTavern未运行,状态码:", response.status_code, n)
|
| 356 |
# 读取 config.ini 中的 monitor_port_enabled 配置项
|
| 357 |
config = configparser.ConfigParser()
|
| 358 |
config.read('/data/config.ini')
|
| 359 |
monitor_port_enabled = config.getboolean('general', 'monitor_port_enabled', fallback=False)
|
| 360 |
if monitor_port_enabled:
|
| 361 |
+
kill_processes_on_port()
|
| 362 |
backup_thread = threading.Thread(target=Run_SillyTavern, daemon=True)
|
| 363 |
backup_thread.start()
|
| 364 |
else:
|
| 365 |
print(f"monitor_port_enabled 为False跳过")
|
|
|
|
| 366 |
|
| 367 |
+
time.sleep(15) # 每5秒检查一次
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
def Run_SillyTavern():
|
|
|
|
| 369 |
try:
|
| 370 |
+
os.chdir('/data/SillyTavern')
|
| 371 |
# 启动 Node.js 服务器,并将输出重定向到 /dev/null
|
| 372 |
+
print('-----启动st-----')
|
| 373 |
+
os.system("yes | sh ./start.sh")
|
| 374 |
+
os.system("yes | sh ./start.sh")
|
| 375 |
+
os.system("yes | sh ./start.sh")
|
| 376 |
+
os.system("yes | sh ./start.sh")
|
| 377 |
+
os.system("yes | sh ./start.sh")
|
| 378 |
+
# subprocess.run(f'yes | sh ./start.sh', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
|
| 379 |
+
# subprocess.run(f'yes | sh {data_folder}/start.sh', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
|
| 380 |
+
# subprocess.run(f'yes | sh {data_folder}/start.sh', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
|
| 381 |
+
# subprocess.run(f'yes | sh {data_folder}/start.sh', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
|
| 382 |
+
# subprocess.run(f'yes | sh {data_folder}/start.sh', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
|
| 383 |
|
| 384 |
print("已尝试启动 server.js")
|
| 385 |
except subprocess.CalledProcessError as e:
|
|
|
|
| 390 |
def SillyTavern():
|
| 391 |
print('-----------SillyTavern开始-------------')
|
| 392 |
# 创建目录并写入 kaggle.json
|
|
|
|
|
|
|
| 393 |
# 打印当前工作目录和用户主目录
|
| 394 |
print("Current Working Directory:", os.getcwd())
|
| 395 |
print("Home Directory:", os.path.expanduser('~'))
|
|
|
|
| 419 |
|
| 420 |
os.system(f"rm -rf /a /data/sillytavern /data/data /data/*.zip /data/*.ctk")
|
| 421 |
# os.system(f"mkdir -p {data_folder}")
|
| 422 |
+
print('----当前文件夹路径--------')
|
| 423 |
+
os.system('pwd')
|
| 424 |
+
print('----进入/data--------')
|
| 425 |
os.chdir('/data')
|
| 426 |
+
|
| 427 |
print(f"kaggle datasets download {huggingface_name}/{huggingface_Data_name}")
|
| 428 |
# os.system(f"kaggle datasets download {huggingface_name}/{huggingface_Data_name}")
|
| 429 |
download_n = 0
|
|
|
|
| 445 |
print(f"文件 /{huggingface_Data_name}.zip 不存在,启用实时同步。")
|
| 446 |
|
| 447 |
else:
|
| 448 |
+
os.chdir('/data')
|
| 449 |
# 如果文件存在,可以继续执行后续代码
|
| 450 |
+
print('----/data文件夹内容--------')
|
| 451 |
+
# os.system("ls")
|
| 452 |
print(f"文件 /data/{huggingface_Data_name}.zip 存在,继续执行程序。")
|
| 453 |
# # 解压 sillytavern.zip
|
| 454 |
# os.system("rm *.zip")
|
| 455 |
os.system(f"7z x /data/{huggingface_Data_name}.zip -y")
|
| 456 |
+
# os.system("mv /data/sillytavern.tck sillytavern.tar.gz")
|
| 457 |
os.system("mv /data/sillytavern.ctk sillytavern.tar.gz")
|
| 458 |
+
# os.system("ls /data")
|
| 459 |
+
# os.system("tar -xvpf ./sillytavern.tar.gz -C / --use-compress-program=pigz")
|
| 460 |
+
# 使用标准 tar 解压
|
| 461 |
+
os.system("tar -xzf sillytavern.tar.gz --strip-components=1 -C /data")
|
| 462 |
# os.system("pv /sillytavern.tar.gz | tar -xvpf - -C / --use-compress-program=pigz")
|
| 463 |
+
# os.system("mv /data/data/SillyTavern /data/SillyTavern")
|
| 464 |
+
|
| 465 |
+
print('更新npm')
|
| 466 |
+
print('----进入/data/SillyTavern--------')
|
| 467 |
os.chdir('/data/SillyTavern')
|
| 468 |
+
os.system("pwd")
|
| 469 |
+
# print('----/data/SillyTavern内容--------')
|
| 470 |
os.system(f"ls {data_folder}")
|
| 471 |
+
os.system("python /data/ip_huggingface.py")
|
| 472 |
os.system("nvm install node")
|
| 473 |
os.system("nvm use node")
|
| 474 |
os.system("npm install -y")
|
| 475 |
+
# os.system("yes | sh ./start.sh")
|
| 476 |
+
# try:
|
| 477 |
+
# subprocess.run(f'yes | sh {data_folder}/start.sh', shell=True, check=True)
|
| 478 |
+
# except subprocess.CalledProcessError as e:
|
| 479 |
+
# print(f"Command '{e.cmd}' returned non-zero exit status {e.returncode}.")
|
| 480 |
+
# print("Output:", e.output)
|
| 481 |
+
# os.system(f"ls {data_folder}")
|
| 482 |
+
|
| 483 |
+
# Run_SillyTavern()
|
| 484 |
backup_thread = threading.Thread(target=Run_SillyTavern, daemon=True)
|
| 485 |
backup_thread.start()
|
| 486 |
# os.system(f"rm -rf /a /data/sillytavern /data/data /data/*.zip /data/*.ctk")
|
| 487 |
+
# os.system("ls /data/")
|
| 488 |
|
| 489 |
print('-----------SillyTavern结束-------------')
|
| 490 |
import requests
|
|
|
|
| 535 |
import psutil
|
| 536 |
|
| 537 |
def check_system_resources():
|
| 538 |
+
print("查看CPU占用情况")
|
| 539 |
+
time.sleep(120)
|
| 540 |
# 获取CPU使用率
|
| 541 |
cpu_usage = psutil.cpu_percent(interval=1)
|
| 542 |
|
|
|
|
| 545 |
memory_usage = memory.percent
|
| 546 |
|
| 547 |
# 检查CPU和内存占用是否超过85%
|
| 548 |
+
if cpu_usage >= 90:
|
| 549 |
+
# if cpu_usage >= 90 or memory_usage >= 90:
|
| 550 |
print("占用过高")
|
| 551 |
# 可选:打印具体的使用率
|
| 552 |
print(f"CPU使用率: {cpu_usage}%")
|
| 553 |
print(f"内存使用率: {memory_usage}%")
|
| 554 |
result = restart_huggingface_space(huggingface_user, huggingface_id, huggingface_tokon)
|
| 555 |
print(result)
|
| 556 |
+
# time.sleep(6666666)
|
| 557 |
else:
|
| 558 |
print("系统资源正常")
|
| 559 |
|
|
|
|
| 565 |
# SillyTavern()
|
| 566 |
time.sleep(60)
|
| 567 |
check_api1()
|
| 568 |
+
# compress_folder()
|
| 569 |
|
| 570 |
# 创建 ConfigParser 对象
|
| 571 |
config = configparser.ConfigParser()
|
|
|
|
| 584 |
Service_status = True
|
| 585 |
frp(configs, True)
|
| 586 |
threading.Thread(target=monitor_port, daemon=True).start()#检测端口状态
|
| 587 |
+
threading.Thread(target=check_system_resources, daemon=True).start()#检测CPU内存状态
|
| 588 |
# time.sleep(600)
|
| 589 |
while True:
|
| 590 |
# 检查是否是中国时区早上6点
|
| 591 |
china_tz = pytz.timezone('Asia/Shanghai')
|
| 592 |
now = datetime.now(china_tz)
|
| 593 |
+
if now.hour == 6 and now.minute < 5: # 6:00-6:05之间退出
|
| 594 |
print(f"当前时间: {now.strftime('%Y-%m-%d %H:%M:%S')}, 到达早上6点,退出循环")
|
| 595 |
break
|
| 596 |
+
# else:
|
| 597 |
+
# print(f"当前时间: {now.strftime('%Y-%m-%d %H:%M:%S')}, 未到达早上6点")
|
| 598 |
|
| 599 |
+
if backup_thread.is_alive() == False:
|
|
|
|
|
|
|
| 600 |
print("备份线程已停止.")
|
| 601 |
backup_thread = threading.Thread(target=repeat_task, daemon=True)
|
| 602 |
backup_thread.start()
|
|
|
|
| 605 |
print(f'{data_folder}/start.sh 文件不存在结束执行')
|
| 606 |
compress_folder()
|
| 607 |
result = restart_huggingface_space(huggingface_user, huggingface_id, huggingface_tokon)
|
| 608 |
+
print(result)
|
|
|