Skip to content

Instantly share code, notes, and snippets.

@Equim-chan
Created July 7, 2017 07:24
Show Gist options
  • Save Equim-chan/bf3cce9ee3b347f40638bbc6624e675b to your computer and use it in GitHub Desktop.
Save Equim-chan/bf3cce9ee3b347f40638bbc6624e675b to your computer and use it in GitHub Desktop.
饥荒服务器启动与 mod 配置的简易脚本
#!/bin/bash
# 饥荒简易启动脚本,附关闭与状态查询
# by Equim
# on 17-07-01
# 饥荒的安装路径
INSTALLED_PATH="$HOME/Steam/steamapps/DST"
# Cluster 所在目录
CLUSTER_PATH="$HOME/.klei/DoNotStarveTogether"
# 保存 PID 的目录
PID_PATH="$HOME/.dst-pid"
COMMAND="$1"
CLUSTER="$2"
mkdir -p $PID_PATH
_printPID() {
echo "主世界 pid 为 $(cat $PID_PATH/$CLUSTER-Master.pid)"
echo "洞穴 pid 为 $(cat $PID_PATH/$CLUSTER-Caves.pid)"
}
status() {
if [[ ! -e $PID_PATH/$CLUSTER-Master.pid ]]; then
echo "Cluster \"$CLUSTER\" 并没有启动"
return
fi
echo "Cluster \"$CLUSTER\" 正在运行"
_printPID
}
start() {
if [[ -e $PID_PATH/$CLUSTER-Master.pid ]]; then
echo "Cluster \"$CLUSTER\" 已经在运行了!"
_printPID
return
fi
cd $INSTALLED_PATH/bin
nohup ./dontstarve_dedicated_server_nullrenderer -cluster $CLUSTER -shard Master \
> /dev/null 2>&1 &
echo $! > $PID_PATH/$CLUSTER-Master.pid
nohup ./dontstarve_dedicated_server_nullrenderer -cluster $CLUSTER -shard Caves \
> /dev/null 2>&1 &
echo $! > $PID_PATH/$CLUSTER-Caves.pid
echo "Cluster \"$CLUSTER\" 已成功启动"
_printPID
}
stop() {
if [[ ! -e $PID_PATH/$CLUSTER-Master.pid ]]; then
echo "Cluster \"$CLUSTER\" 并没有启动"
return
fi
PID0=$(cat $PID_PATH/$CLUSTER-Caves.pid)
PID1=$(cat $PID_PATH/$CLUSTER-Master.pid)
kill -INT $PID0
kill -INT $PID1
echo "正在关闭,请耐心等待..."
ELAPSED=0
while [[ -e /proc/$PID0 && -e /proc/$PID1 ]]; do
sleep .6
ELAPSED=$(($ELAPSED + 1))
# 30 秒阈值
if [[ $ELAPSED -gt 50 ]]; then
kill -KILL $PID0
kill -KILL $PID1
fi
done
echo "Cluster \"$CLUSTER\" 已成功关闭"
rm $PID_PATH/$CLUSTER-Caves.pid
rm $PID_PATH/$CLUSTER-Master.pid
}
restart() {
stop
start
}
clean() {
if [[ -e $PID_PATH/$CLUSTER-Master.pid ]]; then
echo "Cluster \"$CLUSTER\" 正在运行"
echo "如需删档,请先关闭它"
return
fi
echo "接下来要清空 Cluster \"$CLUSTER\" 的所有存档与日志(但会保留配置文件)"
echo -n "该操作不可恢复,确定吗?(y/N) "
read COMFIRM
if [[ "$COMFIRM" == "y" ]]; then
cd $CLUSTER_PATH/$CLUSTER
cd Master
rm -rf save backup
rm -f server_*
cd ../Caves
rm -rf save backup
rm -f server_*
echo "Cluster \"$CLUSTER\" 已成功清档"
fi
}
editMods() {
if [[ $CLUSTER == "" ]]; then
"${EDITOR:-vim}" $INSTALLED_PATH/mods/dedicated_server_mods_setup.lua
else
"${EDITOR:-vim}" $CLUSTER_PATH/$CLUSTER/Master/modoverrides.lua
fi
}
case $COMMAND in
"status")
status
;;
"start")
start
;;
"stop")
stop
;;
"restart")
restart
;;
"clean")
clean
;;
"editMods")
editMods
;;
*)
echo "Usage: ./dst [status|start|stop|restart|clean|editMods] [cluster]"
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment