按照纯净版服务器的部署方法写了一个minecraft+mcrcon在linux上的快速部署脚本
理论上支持deepin系和centos系的系统,目前只完成了ubuntu的测试。
使用需要在minecraft官网获取下载server.jar的链接
mcrcon的使用方式是/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p 密码(有引导设置)
#!/bin/bash
RED="\033[31m" # Error message
colorEcho() {
echo -e "${1}${@:2}${PLAIN}"
}
serverpath="/opt/minecraft/server/"
mcrconpath="/opt/minecraft/tools/mcrcon/"
servicepath="/etc/systemd/system/"
checkSystem() {
uid=$(id -u)
if [[ $uid -ne 0 ]]; then
colorEcho $RED " 请以root身份执行该脚本"
exit 1
fi
res=$(command -v yum)
if [[ "$res" = "" ]]; then
res=$(command -v apt)
if [[ "$res" = "" ]]; then
colorEcho $RED " 不受支持的Linux系统"
exit 1
fi
PMT="apt"
CMD_INSTALL="apt install -y "
CMD_UPGRADE="apt update ; apt upgrade -y ; apt autoremove -y"
else
PMT="yum"
CMD_INSTALL="yum install -y "
CMD_UPGRADE="yum update -y"
fi
res=$(command -v systemctl)
if [[ "$res" = "" ]]; then
colorEcho $RED " 系统版本过低,请升级到最新版本"
exit 1
fi
}
prepare(){
$CMD_UPGRADE
$CMD_INSTALL curl wget openjdk-17-jdk git nano vim build-essential
useradd -r -m -U -d /opt/minecraft -s /bin/bash minecraft
su - minecraft -c "mkdir -p ~/{tools,server}"
}
mcrcon_b(){
su - minecraft -c "cd ~/tools && git clone https://github.com/Tiiffi/mcrcon.git"
su - minecraft -c "cd ~/tools/mcrcon && gcc -std=gnu11 -pedantic -Wall -Wextra -O2 -s -o mcrcon mcrcon.c"
alias mcrcon="/opt/minecraft/tools/mcrcon/mcrcon"
}
server_jar(){
read -p "请将server.jar下载链接复制到此处:" server
su - minecraft -c "wget $server -P $serverpath"
read -p "请输入希望给minecraft分配的最大内存(MB):" Max
cd $serverpath
su - minecraft -c "cd ${serverpath} && java -Xms512M -Xmx${Max}M -jar server.jar nogui"
sed -i "s|false|true|g" ${serverpath}eula.txt
read -p "请输入mcrcon管理密码:" mcrcon_passwd
sed -i "s|strong-password|$mcrcon_passwd|g" ${serverpath}server.properties
sed -i "s|enable-rcon=false|enable-rcon=true|g" ${serverpath}server.properties
}
service(){
echo '[Unit]
Description=Minecraft Server
After=network.target
[Service]
User=minecraft
Nice=1
KillMode=none
SuccessExitStatus=0 1
ProtectHome=true
ProtectSystem=full
PrivateDevices=true
NoNewPrivileges=true
WorkingDirectory=/opt/minecraft/server
ExecStart=/usr/bin/java -XmxTTTM -Xms512M -jar server.jar nogui
ExecStop=/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strong-password stop
[Install]
WantedBy=multi-user.target' >> ${servicepath}minecraft.service
sed -i "s|TTT|$Max|g" ${servicepath}minecraft.service
sed -i "s|strong-password|$mcrcon_passwd|g" ${servicepath}minecraft.service
read -p "是否允许开机自启动[y/n]:" res
if [[ "$res" = "y" ]]; then
systemctl enable minecraft.service
fi
}
main(){
checkSystem
prepare
mcrcon_b
server_jar
service
}
main