当前位置:   article > 正文

幻兽帕鲁服务器自动重启备份-python_帕鲁专服怎么自动重启

帕鲁专服怎么自动重启

基于python编写的服务器全自动管理工具,能够实现自动定时备份存档,以及在检测到服务器崩溃之后自动重新启动,并且整合了对于frp端口转发工具的自动重启。

我受够这个服务器没完没了的崩溃了,别再整天艾特我开服了

如果对你的部署很有用,欢迎评论和点赞~

1. 前置知识点

幻兽帕鲁开服教程——游戏
架设游戏私服——内网穿透工具frp

需要掌握基本python编程知识,知道怎么部署python环境与修改配置路径。

2. 目录结构

|-pal_server_manage.bat
|-pal_server_manage.py
  • 1
  • 2

3. 代码内容

pal_server_manage.bat
这就是一个单纯方便双击启动运行的脚本。

python D:\servers\pal_server_manage.py
  • 1

pal_server_manage.py
包含了初始化启动、自动备份与自动重启的功能。

import os
import time
import zipfile
import socket
import threading
import psutil
import subprocess


# 参数配置
class Config:
    # 服务器路径
    server_path = r"D:\servers\steamcmd\steamapps\common\PalServer\PalServer.exe"

    # 计算服务器应用程序名字
    server_name = os.path.split(server_path)[-1]

    # frp路径
    frp_path = r"D:\servers\frp\client\frpc.exe"
    frp_config = r"D:\servers\frp\client\frpc.ini"
    frp_name = os.path.split(frp_path)[-1]

    # 记录正在运行的服务器
    server = None
    frp = None

    # 是否使用自动重启
    use_auto_restart = True
    # 检测服务器是否在运行的间隔(秒)
    check_server_run_step = 10

    # 是否启动自动备份
    use_auto_backup = True
    # 备份的路径
    save_dir_path = r"D:\servers\steamcmd\steamapps\common\PalServer\Pal\Saved"
    # 备份的时间间隔(秒)
    save_time_step = 900
    # 备份的存档路径
    output_zip_path = r"D:\servers\save_backups\pal_save_backups"
    os.makedirs(os.path.split(output_zip_path)[-1], exist_ok=True)


# 将1个文件夹打包压缩为zip文件
def zip_file(src_dir, zip_path):
    z = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)
    count = 0
    for dirpath, _, filenames in os.walk(src_dir):
        fpath = dirpath.replace(src_dir, '')
        fpath = fpath and fpath + os.sep or ''
        length = len(filenames)
        for filename in filenames:
            z.write(os.path.join(dirpath, filename), os.path.split(src_dir)[1] + fpath + filename)
            count += 1
            print(f'\rzip file: {count}/{length}', end='')
    print(f'\nzip {src_dir} success!')
    z.close()


# 检测程序是否在运行
def is_program_running(program_name):
    # 扫描所有的进程id
    for pid in psutil.pids():
        try:
            # 如果进程名与服务器名一致,代表服务器正在运行
            if psutil.Process(pid).name() == program_name:
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            pass
    # 扫描所有进程后,未找到服务器
    return False


# 获取当前可视化时间信息
def get_local_time():
    local = time.localtime(time.time())
    now = f"{local[0]:04d}_{local[1]:02d}_{local[2]:02d}_{local[3]:02d}_{local[4]:02d}_{local[5]:02d}"
    return now


# 自动备份线程
class auto_server_backup(threading.Thread):
    def __init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name

    def run(self):
        while True:
            # 压缩存档文件夹到备份路径
            now = get_local_time()
            zip_path = os.path.join(Config.output_zip_path, now + ".zip")
            zip_file(Config.save_dir_path, zip_path)
            print(f"{now}: zip file from {Config.save_dir_path} to {zip_path}")
            # 休眠等待
            time.sleep(Config.save_time_step)


# 自动重启线程
class auto_server_restart(threading.Thread):
    def __init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name

    def run(self):
        while True:
            # 如果frp没有启动,就启动它
            if not is_program_running(Config.frp_name):
                now = get_local_time()
                Config.frp = subprocess.Popen(["start", Config.frp_path, "-c", Config.frp_config])
                print(f"{now}: Restart frp {Config.frp_path}")

            # 如果服务器没有启动,就启动它
            if not is_program_running(Config.server_name):
                now = get_local_time()
                Config.server = subprocess.Popen([Config.server_path])
                print(f"{now}: Restart Server {Config.server_path}")

            # 休眠等待
            time.sleep(Config.check_server_run_step)


def main():
    # 检查服务器是否启动
    # 如果frp没有启动,就启动它
    if not is_program_running(Config.frp_name):
        Config.frp = subprocess.Popen([Config.frp_path, "-c", Config.frp_config])
        print(f"Start frp {Config.frp_path}")
    # 如果服务器没有启动,就启动它
    if not is_program_running(Config.server_name):
        Config.server = subprocess.Popen([Config.server_path])
        print(f"Start Server {Config.server_path}")

    # 自动保存线程
    if Config.use_auto_backup:
        thread_backup = auto_server_backup(1, "backup")
        thread_backup.start()

    # 自动重启线程
    if Config.use_auto_restart:
        thread_restart = auto_server_restart(2, "restart")
        thread_restart.start()

    # 在子线程结束前不要终止,也就是无限堵塞
    if Config.use_auto_backup:
        thread_backup.join()
    if Config.use_auto_restart:
        thread_restart.join()


if __name__ == "__main__":
    main()
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153

4. 原理解释

自动备份:定时压缩服务器存档文件夹到备份路径,非常简单粗暴。
注:因为没有在服务器内部运行保存命令,有极小概率可能出现玩家与世界存档不同步的问题,但是因为发生概率太低而且只要加快保存频率就不是什么大问题(其实是懒得整那么麻烦的东西),所以选择性无视了此问题。

自动保存:定时扫描所有进程,检测服务器是否在运行,发现没有在运行,就重新启动~

5. 额外备注

如果你希望主动定时关闭服务器重启,可以用该代码主动关闭服务器:

Config.server.kill()
  • 1

祝大家玩得开心呀~上班当帕鲁已经够苦了,下班后都开心点吧!
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/181850
推荐阅读
相关标签
  

闽ICP备14008679号