异常问题解决
问题1
ERR [EOS] Failed unregistering player in session: NotFound
解决办法:
到 https://api.epicgames.dev/sdk/v1/default?platformId=WIN 中下载证书,并到服务器上安装
教程:
https://www.bilibili.com/video/BV1jx4y1t7LG/?spm_id_from=333.337.search-card.all.click&vd_source=e2984b2e17c2ace1cd065cedfe2c279c
自动程序
通过Python实现程序自动重启,且重启时自动更新,支持将配置文件的副本拷贝到指定位置防止更新时刷新配置文件
import subprocess
import psutil
import shutil
import schedule
import time
import os
import sys
program_name = "7DaysToDieServer.exe" # 程序名
program_directory = "C:\steamcmd\\7daytodie" # 程序目录
program_start_command = "startdedicated.bat" # 程序启动命令
steam_app_path = "./7DayToDie" # 指定程序安装目录
steam_app_id = "294420" # 程序id
xml_list = [
{
"source": "C:\steamcmd\\7daytodie\serverconfig - 副本.xml",
"target": "C:\steamcmd\\7daytodie\serverconfig.xml"
}, {
"source": "C:\steamcmd\\7daytodie\progression - 副本.xml",
"target": "C:\steamcmd\\7daytodie\Data\Config\progression.xml"
}
]
# 检测程序是否在执行
def check_process_running(process_name):
for process in psutil.process_iter(['name', 'cmdline']):
if process.info['name'] and process.info['name'] == process_name:
return True
elif process.info['cmdline'] and process_name in process.info['cmdline']:
return True
return False
# 执行steamcmd更新命令
def run_steamcmd_update_command_realtime():
# 运行SteamCMD命令并实时获取输出
process = subprocess.Popen(['steamcmd.exe', '+login', 'anonymous', '+force_install_dir', steam_app_path, '+app_update', steam_app_id, 'validate', '+quit'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
encoding='utf-8')
# 实时获取控制台输出内容
for line in process.stdout:
print(line, end='')
# 等待命令执行完毕
process.wait()
# 检查是否有错误发生
if process.returncode != 0:
print(f"Command execution failed with exit code {process.returncode}")
# 拷贝配置文件,防止更新时配置文件被还原
for xml in xml_list:
try:
source = xml["source"]
target = xml["target"]
shutil.copy2(source, target)
print(f"拷贝{source}配置文件到{target}成功!")
except Exception as e:
print(f"拷贝{source}配置文件到{target}失败:", str(e))
# 关闭服务器
def stop_program():
print("服务器关闭时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
if sys.platform.startswith('win'):
subprocess.Popen('taskkill /F /IM ' + program_name, shell=True)
# elif sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
# subprocess.Popen(['pkill', '-f', 'your_program.py'])
# 启动服务器
def start_program():
print("服务器启动时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
if sys.platform.startswith('win'):
# subprocess.Popen(program_path, shell=True)
# subprocess.run([program_start_command], input=b'\n', shell=True, cwd=program_directory)
process = subprocess.Popen([program_start_command],
shell=True,
cwd=program_directory,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
encoding='gbk')
# # 实时获取控制台输出内容
# for line in process.stdout:
# out = process.stdout.readline()
# print(out, end='')
# if out == '' and process.poll() is not None:
# break
# elif '请按任意键继续. . .' in out:
# # 当输出中出现特定内容时
# process.stdin.write('\n')
# process.stdin.flush() # 确保输入被发送
# #
time.sleep(10) # 等待10秒
# 强制终止子进程
process.kill()
# 等待进程结束
process.wait()
# 检查是否有错误发生
if process.returncode != 0:
print(f"Command execution failed with exit code {process.returncode}")
# elif sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
# subprocess.Popen(['python', 'your_program.py'])
# 重启任务
def stop_and_restart_job():
while check_process_running(program_name): # 等待程序结束完成
stop_program()
time.sleep(10) # 等待10秒,确保程序完全关闭
print("启动程序更新")
run_steamcmd_update_command_realtime()
while not check_process_running(program_name): # 查询程序是否启动,如果未启动则再次启动
start_program()
time.sleep(60)
# 定义任务调度函数
def schedule_job():
# schedule.every(30).minutes.do(stop_and_restart_job) # 每隔半小时执行一次
schedule.every().day.at("11:53").do(stop_and_restart_job) # 每天11:53重启服务器
# 启动任务调度器
schedule_job()
print(f"检测{program_name}程序是否在执行:{check_process_running(program_name)}")
# if not check_process_running(program_name):
# print("程序未执行,启动程序更新")
# run_steamcmd_update_command_realtime()
# start_program()
stop_and_restart_job()
while True:
schedule.run_pending() # 检查是否有任务需要执行
time.sleep(1) # 避免过于频繁的检查