因為這是Wiki的一個條目,可以在任何時間內被任何人編輯,因此建議你不要完全使用這個腳本,而是將其當作編寫腳本的指導手冊看待。
這是一個GNU/Linux發行版的Minecraft伺服器端的啟動和維護腳本示例。
Systemd腳本
此方法適用於支持Systemd的所有現代伺服器。Systemd是一種「萬金油」,用於各種需求。Ubuntu在15.04版引入了它。(此版本已停止支持,建議使用更高的LTS版本,但是以下內容能正常在此版本中的Systemd上工作。)
安裝
- 連接到你的(v)root伺服器,或者如果要在你的機器上執行伺服器,打開一個終端。
- 使用su或sudo指令切換為root使用者。要檢查目前是否為root,執行
id -u如果返回0,則是root。 - 接下來安裝一些必需的包:
apt install openjdk-8-jre-headless curl screen nano bash grep - 如果不存在
/opt目錄,則建立它:mkdir /opt - 現在你需要為此服務建立一個使用者:
adduser --system --shell /bin/bash --home /opt/minecraft --group minecraft - 建立Systemd Unit檔案:
nano /etc/systemd/system/minecraft@.service,內容如下:
# Source: https://github.com/agowa338/MinecraftSystemdUnit/ # License: MIT [Unit] Description=Minecraft Server %i After=network.target [Service] WorkingDirectory=/opt/minecraft/%i PrivateUsers=true # Users Database is not available for within the unit, only root and minecraft is available, everybody else is nobody User=minecraft Group=minecraft ProtectSystem=full # Read only mapping of /usr /boot and /etc ProtectHome=true # /home, /root and /run/user seem to be empty from within the unit. It is recommended to enable this setting for all long-running services (in particular network-facing ones). ProtectKernelTunables=true # /proc/sys, /sys, /proc/sysrq-trigger, /proc/latency_stats, /proc/acpi, /proc/timer_stats, /proc/fs and /proc/irq will be read-only within the unit. It is recommended to turn this on for most services. # Implies MountFlags=slave ProtectKernelModules=true # Block module system calls, also /usr/lib/modules. It is recommended to turn this on for most services that do not need special file systems or extra kernel modules to work # Implies NoNewPrivileges=yes ProtectControlGroups=true # It is hence recommended to turn this on for most services. # Implies MountAPIVFS=yes ExecStart=/bin/sh -c '/usr/bin/screen -DmS mc-%i /usr/bin/java -server -Xms512M -Xmx2048M -XX:+UseG1GC -XX:+CMSIncrementalPacing -XX:+CMSClassUnloadingEnabled -XX:ParallelGCThreads=2 -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -jar $(ls -v | grep -i "FTBServer.*jar\|minecraft_server.*jar" | head -n 1) nogui' ExecReload=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "reload"\\015' ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN. Saving map..."\\015' ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "save-all"\\015' ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"\\015' ExecStop=/bin/sleep 10 Restart=on-failure RestartSec=60s [Install] WantedBy=multi-user.target ######### # HowTo ######### # # Create a directory in /opt/minecraft/XX where XX is a name like 'survival' # Add minecraft_server.jar into dir with other conf files for minecraft server # # Enable/Start systemd service # systemctl enable minecraft@survival # systemctl start minecraft@survival # # To run multiple servers simply create a new dir structure and enable/start it # systemctl enable minecraft@creative # systemctl start minecraft@creative
配置實例
現在你可以上傳你的FTB整合包到名為/opt/minecraft/的子資料夾。例如將"FTB Beyond"整合包放進/opt/minecraft/FTBBeyond(不帶空格)。
如果你想執行原版實例,只需要在/opt/minecraft內建立資料夾,上傳minecraft_server.jar並建立eula.txt檔案(使用:)。
上傳minecraft伺服器端檔案後,使用ls -la /opt/minecraft指令確保「minecraft」是其所有者和所屬組。若不是,則執行chown minecraft:minecraft /opt/minecraft/FTBBeyond。你也許還需要完成安裝。對於目前的FTB包你可以執行:
cd /opt/minecraft/FTBBeyond echo "eula=true" > /opt/minecraft/FTBBeyond/eula.txt su -c "/opt/minecraft/FTBBeyond/FTBInstall.sh" -s "/bin/bash" minecraft
啟動/停止實例
你可以使用systemctl start minecraft@FTBBeyond來啟動伺服器並用systemctl stop minecraft@FTBBeyond來關閉伺服器。「@」後的部分是一個實例名稱,如資料夾名。該腳本也會在重啟伺服器時幫助自動停止你的Minecraft伺服器。
自啟動
啟用
systemctl enable minecraft@FTBBeyond
停用
systemctl disable minecraft@FTBBeyond
Init.d腳本
用於不內建Systemd的舊版伺服器系統。
先決條件
必須安裝Screen包。
在CentOS及基於Red Hat的發行版上:
yum install screen
在Ubuntu等基於Debian的系統上:
apt-get install screen python
下載
使用wget下載本腳本,執行下列指令:(注意本腳本需要變更WORLD、MCPATH和BACKUPPATH變數。
重要說明: 如果你使用了wget的方法而每行第一個字元都是空格,本腳本不會執行並且update-rc.d會輸出錯誤。如果這要,你必須移除每行開頭的空格。小心不要刪除空格以外的任何東西!
wget -O minecraft "http://minecraft.gamepedia.com/Tutorials/Server_startup_script/Script?action=raw"
#!/bin/bash
# /etc/init.d/minecraft
# version 0.4.2 2016-02-09 (YYYY-MM-DD)
#
### BEGIN INIT INFO
# Provides: minecraft
# Required-Start: $local_fs $remote_fs screen-cleanup
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Minecraft server
# Description: Starts the minecraft server
### END INIT INFO
#Settings
SERVICE='minecraft_server.jar'
SCREENNAME='minecraft_server'
OPTIONS='nogui'
USERNAME='minecraft'
WORLD='world'
MCPATH='/home/minecraft'
BACKUPPATH='/minecraft/minecraft.backup'
MAXHEAP=2048
MINHEAP=1024
HISTORY=1024
CPU_COUNT=1
INVOCATION="java -Xmx${MAXHEAP}M -Xms${MINHEAP}M -XX:+UseConcMarkSweepGC \
-XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts \
-jar $SERVICE $OPTIONS"
ME=`whoami`
as_user() {
if [ "$ME" = "$USERNAME" ] ; then
bash -c "$1"
else
su - "$USERNAME" -c "$1"
fi
}
mc_start() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is already running!"
else
echo "Starting $SERVICE..."
cd $MCPATH
as_user "cd $MCPATH && screen -h $HISTORY -dmS ${SCREENNAME} $INVOCATION"
sleep 7
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is now running."
else
echo "Error! Could not start $SERVICE!"
fi
fi
}
mc_saveoff() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is running... suspending saves"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"say SERVER BACKUP STARTING. Server going readonly...\"\015'"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-off\"\015'"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-all\"\015'"
sync
sleep 10
else
echo "$SERVICE is not running. Not suspending saves."
fi
}
mc_saveon() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is running... re-enabling saves"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-on\"\015'"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"say SERVER BACKUP ENDED. Server going read-write...\"\015'"
else
echo "$SERVICE is not running. Not resuming saves."
fi
}
mc_stop() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "Stopping $SERVICE"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-all\"\015'"
sleep 10
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"stop\"\015'"
sleep 7
else
echo "$SERVICE was not running."
fi
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "Error! $SERVICE could not be stopped."
else
echo "$SERVICE is stopped."
fi
}
mc_update() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is running! Will not start update."
else
as_user "cd $MCPATH && wget -q -O $MCPATH/versions --no-check-certificate https://piston-meta.mojang.com/mc/game/version_manifest.json"
if [ "$1" == "snapshot" ] ; then
JSONVERSION=`cd $MCPATH && cat versions | python -c "exec(\"import json,sys\nobj=json.load(sys.stdin)\nversion=obj['latest']['snapshot']\nfor v in obj['versions']:\n if v['id']==version:\n print(v['url'])\")"`
else
JSONVERSION=`cd $MCPATH && cat versions | python -c "exec(\"import json,sys\nobj=json.load(sys.stdin)\nversion=obj['latest']['release']\nfor v in obj['versions']:\n if v['id']==version:\n print(v['url'])\")"`
fi
as_user "cd $MCPATH && wget -q -O $MCPATH/versions --no-check-certificate $JSONVERSION"
MC_SERVER_URL=`cd $MCPATH && cat versions | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["downloads"]["server"]["url"])'`
as_user "rm $MCPATH/versions"
as_user "cd $MCPATH && wget -q -O $MCPATH/minecraft_server.jar.update --no-check-certificate $MC_SERVER_URL"
if [ -f $MCPATH/minecraft_server.jar.update ] ; then
if `diff $MCPATH/$SERVICE $MCPATH/minecraft_server.jar.update >/dev/null` ; then
echo "You are already running the latest version of $SERVICE."
else
as_user "mv $MCPATH/minecraft_server.jar.update $MCPATH/$SERVICE"
echo "Minecraft successfully updated."
fi
else
echo "Minecraft update could not be downloaded."
fi
fi
}
mc_backup() {
mc_saveoff
NOW=`date "+%Y-%m-%d_%Hh%M"`
BACKUP_FILE="$BACKUPPATH/${WORLD}_${NOW}.tar"
echo "Backing up minecraft world..."
#as_user "cd $MCPATH && cp -r $WORLD $BACKUPPATH/${WORLD}_`date "+%Y.%m.%d_%H.%M"`"
as_user "tar -C \"$MCPATH\" -cf \"$BACKUP_FILE\" $WORLD"
echo "Backing up $SERVICE"
as_user "tar -C \"$MCPATH\" -rf \"$BACKUP_FILE\" $SERVICE"
#as_user "cp \"$MCPATH/$SERVICE\" \"$BACKUPPATH/minecraft_server_${NOW}.jar\""
mc_saveon
echo "Compressing backup..."
as_user "gzip -f \"$BACKUP_FILE\""
echo "Done."
}
mc_command() {
command="$1";
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
pre_log_len=`wc -l "$MCPATH/logs/latest.log" | awk '{print $1}'`
echo "$SERVICE is running... executing command"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"$command\"\015'"
sleep .1 # assumes that the command will run and print to the log file in less than .1 seconds
# print output
tail -n $((`wc -l "$MCPATH/logs/latest.log" | awk '{print $1}'`-$pre_log_len)) "$MCPATH/logs/latest.log"
fi
}
mc_listen() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
as_user "tail -f $MCPATH/logs/latest.log"
else
echo "$SERVICE is not running. Cannot listen to server."
fi
}
#Start-Stop here
case "$1" in
start)
mc_start
;;
stop)
mc_stop
;;
restart)
mc_stop
mc_start
;;
update)
mc_stop
mc_backup
mc_update $2
mc_start
;;
backup)
mc_backup
;;
status)
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
command)
if [ $# -gt 1 ] ; then
shift
mc_command "$*"
else
echo "Must specify server command (try 'help'?)"
fi
;;
listen)
mc_listen
;;
*)
echo "Usage: $0 {start|stop|update|backup|status|restart|command \"server command\"}"
exit 1
;;
esac
exit 0
軟體需求
- screen
- python (
apt-get install python)
安裝
使用你所好的編輯器在/etc/init.d/下建立叫做minecraft的檔案並將上面的腳本粘貼進該檔案。
根據你的配置編輯USERNAME和MCPATH變數。如果你想使用外覆腳本,變更INVOCATION來啟動而不是直接從伺服器端啟動。
確保新建立的檔案擁有必需的權限,你可以以此設定權限:
chmod a+x /etc/init.d/minecraft
然後執行(在基於Debian的發行版)
update-rc.d minecraft defaults
若使用啟用了基於依賴的啟動(dependency-based booting)的Debian 6.0啟動,則使用insserv指令。若一切正常,insserv將沒有輸出。若想確認,檢查$?中的錯誤碼。
insserv minecraft
在CentOS和RHEL(Redhat企業版Linux)上,你將需要將進程加入至chkconfig列表,由chkconfig在systemd下管理啟動腳本。
chkconfig --add minecraft
要檢查該進程是否正確完成,使用ntsysv指令來持續捲動直至看到minecraft進程(如果不重複chkconfig指令來加入所需的符號連結)。
注意: 你的系統很可能會警告你該腳本不滿足所有需求。然而腳本還是會執行。
你也可以在你的crontab下配置條目來備份伺服器端。一個crontab例子是每半小時備份一次:
- 使用你希望完成工作的使用者帳戶,執行:
crontab -e
並加入這個:
0,30 * * * * /etc/init.d/minecraft backup
如果由於你不知道如何使用vi而導致上述嘗試失敗,嘗試:
VISUAL=/usr/bin/nano crontab -e
卸載
(在基於debian的GNU/Linux發行版中)
update-rc.d -f minecraft remove
(在CentOS/RHEL中)
chkconfig --del minecraft
用法
在大多數系統上可以透過以下指令調用腳本,其中「(command)」可以是「stop」、「start」、「restart」或任何所支持的其他選項:
/etc/init.d/minecraft (command)
在大多數基於RedHat或Debian的發行版中,可以使用service指令,以此調用:
service minecraft (command)
要查看畫面,使用:
screen -r
要退出畫面,使用:
CTRL+a+d
參考
- http://www.debian-administration.org/articles/28
- https://wiki.debian.org/LSBInitScripts/DependencyBasedBoot
附加資訊
如果仍要查看實時日誌檔案,請在伺服器目錄中使用此指令:
tail -f logs/latest.log
備選啟動腳本
以下的這些腳本提供的函數與上述腳本相同,但包含了更多有用的特性:
- [Multi World] Minecraft Server Control Script
- 執行多個Minecraft世界。
- 啟動、停止和重啟多個世界。
- 建立、刪除、停用和啟用多個世界。
- 相比於Mojang伺服器端,額外支持CraftBukkit。
- 自動通知使用者重要的伺服器事件。
- 使用Minecraft Query protocol來保持目前伺服器狀態的追蹤。
- LSB和systemd相容的init腳本允許與伺服器的啟動和關閉序列無縫集成。
- 使用Minecraft Overviewer地圖繪製軟體繪製世界。
- 備份世界並移除超過X天的備份。
- 更新伺服器端軟體並安裝addons。
- 從命令列向世界伺服器發送指令。
- minecraftd
- 使用systemd或腳本直接啟動、停止和重新啟動伺服器
- 從命令列向伺服器發送指令
- 備份伺服器(世界、外掛、配置等)和清除舊伺服器(可配置)
- 如果沒有玩家登入則掛起伺服器,並在嘗試立即啟動它至最大效率
- 純粹用大約500行代碼的bash和濃縮特性編寫,以保持較小的足跡
- 靈活的配置:支持例如 spigot和craftbukkit,可調節執行緒和RAM使用等。
- 具有初始化和備份腳本的完整systemd支持
- 安全資源使用:以不同使用者身份執行腳本,如果不需要則刪除權限
- 與本文中描述的腳本非常相似:它也使用畫面和tar,但提供更高級的功能
- 非凡的Arch Linux支持
- Minecraft Systemd Service 一個很好的系統服務,具有:
- 使用rcon安全關機
- 透過使大部分系統只讀來保護系統
- 使用systemd日誌記錄
- 可以與一個不錯的commandcenter腳本結合使用
- 完全集成在systemd-toolchain中
- minecraft init
- 使用多世界等更多功能修改此腳本
- Mineserv Perl Init Script
- 一個非常簡單的自啟動/停止腳本,具有備份和清理功能,並能夠將指令傳遞到伺服器控制台。
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
語言