您的位置:首页 > 教育 > 锐评 > shell脚本定时重启游戏服务器

shell脚本定时重启游戏服务器

2024/10/6 2:27:26 来源:https://blog.csdn.net/q277055799/article/details/141895041  浏览:    关键词:shell脚本定时重启游戏服务器

背景故事:入职了一套烂代码。内存泄露很严重,改不动,解决方案就是每天凌晨4点重启一次,技术上是共享内存,实时修改的,重启不会丢数据。

1.启动服务器脚本 start.sh

chmod 777 *
nohup `pwd`/DataManageServer 0 &
sleep 1
nohup `pwd`/MonitorServer &
nohup `pwd`/LogicServer &
nohup `pwd`/ChatServer &
nohup `pwd`/BattleServer &
nohup `pwd`/ChatLoginServer &
nohup `pwd`/LoginServer &

2.重启服务器脚本 new_sg.sh

#!/bin/bash
###启动服务器脚本
ls -l | grep ^decho "start dir-->" $1
if [ $# = 0 ]; thenecho "input start dir"exit
fiif [ ! -d $1 ]; thenecho $1 "is not dir"exit
fi#一个一个杀,如果杀整个文件夹,会把sh自己杀掉导致提前结束
ps -ef | grep $1/DataManageServer | cut -c 9-15 | xargs kill -9
ps -ef | grep $1/MonitorServer | cut -c 9-15 | xargs kill -9
ps -ef | grep $1/LogicServer | cut -c 9-15 | xargs kill -9
ps -ef | grep $1/ChatServer | cut -c 9-15 | xargs kill -9
ps -ef | grep $1/BattleServer | cut -c 9-15 | xargs kill -9
ps -ef | grep $1/ChatLoginServer | cut -c 9-15 | xargs kill -9
ps -ef | grep $1/LoginServer | cut -c 9-15 | xargs kill -9cd $1
sh start.sh
ps aux | grep LogicServer | grep -v "grep" | grep -c $1
ipcs
cd -

3.重启本机所有服脚本 sg_all.sh

#!/bin/bash
sh new_sg.sh  sgs401 &
#sh new_sg.sh  sgs306 &

4.定时重启脚本 keep.sh

#!/bin/sh# 获取格式化的系统时间
formatted_time=$(date +"%Y-%m-%d %H:%M:%S")
# 打印格式化的系统时间
#echo "当前系统时间格式化后是: $formatted_time"restart_time=14410
#restart_time=34740
while true 
do# 获取当前时间的时间戳seconds=$(date +"%s")	# 获取当天0点的秒数midnight_seconds=$(date -d "today 00:00:00" +"%s")# 计算从0点到现在的秒数elapsed_seconds=$((seconds - midnight_seconds))	#echo "$restart_time $elapsed_seconds"if [ "$elapsed_seconds" -eq "$restart_time" ]; thensh sg_all.sh &fisleep 1
done

启动定时脚本,注意一定要后台启动,不然窗口关闭就不在了

nohup sh keep.sh &

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com