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

Update server.py

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