背景故事:入职了一套烂代码。内存泄露很严重,改不动,解决方案就是每天凌晨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 &