Qilan2 commited on
Commit
99e6f0c
·
verified ·
1 Parent(s): 87a68bb

Update server2.py

Browse files
Files changed (1) hide show
  1. server2.py +11 -20
server2.py CHANGED
@@ -451,28 +451,16 @@ def replace_ip_and_country_in_file(file_path):
451
  except Exception as e:
452
  print(f"发生其他错误: {e}")
453
 
454
- def is_script_running(script_name):
455
- try:
456
- # 使用 pgrep 查找正在运行的进程
457
- result = subprocess.run(
458
- ["pgrep", "-f", script_name],
459
- stdout=subprocess.PIPE,
460
- stderr=subprocess.PIPE,
461
- text=True
462
- )
463
-
464
- # 检查返回码和输出
465
- if result.returncode == 0:
466
- print(f"{script_name} 正在运行,进程 ID: {result.stdout.strip()}")
467
  return True
468
  else:
469
- print(f"{script_name} 未在运行,进行重启")
470
- subprocess.run('yes | sh /sillytavern/start.sh', shell=True, check=True)
471
  return False
472
-
473
- except Exception as e:
474
- print(f"发生错误: {e}")
475
- return False
476
 
477
  def SillyTavern():
478
  print('-----------SillyTavern开始-------------')
@@ -556,7 +544,10 @@ if os.path.exists('/sillytavern/start.sh'):
556
  frp_thread2 = threading.Thread(target=frp, args=(config_data, '/content/frp/frpc.ini', False))
557
  frp_thread2.start()
558
  while True:
559
- is_running = is_script_running("/sillytavern/start.sh")
 
 
 
560
  if backup_thread.is_alive():
561
  print("备份线程仍在运行...")
562
  else:
 
451
  except Exception as e:
452
  print(f"发生其他错误: {e}")
453
 
454
+ def is_port_in_use(port):
455
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
456
+ # 尝试绑定到指定的端口
457
+ result = sock.connect_ex(('localhost', port))
458
+ if result == 0:
459
+ print(f"端口 {port} 正在使用中")
 
 
 
 
 
 
 
460
  return True
461
  else:
462
+ print(f"端口 {port} 未被使用")
 
463
  return False
 
 
 
 
464
 
465
  def SillyTavern():
466
  print('-----------SillyTavern开始-------------')
 
544
  frp_thread2 = threading.Thread(target=frp, args=(config_data, '/content/frp/frpc.ini', False))
545
  frp_thread2.start()
546
  while True:
547
+ if not is_port_in_use(8000):
548
+ os.chdir('/sillytavern')
549
+ subprocess.Popen('yes | sh /sillytavern/start.sh', shell=True)
550
+ time.sleep(15)
551
  if backup_thread.is_alive():
552
  print("备份线程仍在运行...")
553
  else: