Minecraft Wiki

除另有声明,转载时均必须注明出处若簡繁轉換出錯,請以遊戲內為準请勇于扩充与修正内容有兴趣逛逛我们的微博沟通交流,欢迎到社区专页需要协助,请在告示板留言

了解更多

Minecraft Wiki
Advertisement
警告

因為這是Wiki的一個條目,可以在任何時間內被任何人編輯,因此建議你不要完全使用這個腳本,而是將其當作編寫腳本的指導手冊看待。


這是一個 Minecraft 伺服器 rc.d 的 FreeBSD 腳本示例。

安裝

  • 使用 adduser 來建立一個名為 minecraft 的新使用者,新使用者根目錄指定為 /srv/minecraft
  • 保證你的 /srv 檔案系統擁有足夠的空間(約 100-200MB ,取決於你的需要) - df -h 將會顯示有多少可用空間。
  • 如果必要,安裝 Java。端口在 /usr/ports/java/jdk16
  • 在 root 執行:
# cd /usr/ports/sysutils/screen && make install clean
# cd /srv/minecraft
# fetch https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
# chown minecraft:minecraft *
# fetch -o /usr/local/etc/rc.d/minecraft http://vidya.dyndns.org/stuff/minecraft
# chmod 0555 /usr/local/etc/rc.d/minecraft
  • If you have existing server configuration files, world files, etc., copy them into /srv/minecraft and give ownership to your minecraft user.
  • Edit /etc/rc.conf.local (create if it doesn't exist) and add the following line:
minecraft_enable="yes"
  • The server should now start on boot. To launch, check status, and shut down, use the following commands:
# /usr/local/etc/rc.d/minecraft start
# /usr/local/etc/rc.d/minecraft status
# /usr/local/etc/rc.d/minecraft stop

Or:

# service minecraft start
# service minecraft status
# service minecraft stop

Script

#!/bin/sh
#
# PROVIDE: minecraft
# REQUIRE: LOGIN DAEMON NETWORKING mountcritlocal
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local to enable the minecraft server:
#
# minecraft_enable="YES"
# minecraft_user="<run server as this user>"
# minecraft_chdir="<run server in this directory>"
# minecraft_path="<path to minecraft_server.jar>"
# minecraft_flags="<set as needed>"
#
# For default setup, create a user named 'minecraft', set its home directory
# to /srv/minecraft, and place minecraft_server.jar into /srv/minecraft
#
# See minecraft_server.jar for flags

. /etc/rc.subr

name=minecraft
rcvar=minecraft_enable

load_rc_config ${name}

command=/usr/local/bin/screen
pidfile=/var/run/minecraft.pid

start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"

: ${minecraft_enable="NO"}
: ${minecraft_session="minecraft-session"}
: ${minecraft_user="minecraft"}
: ${minecraft_chdir="/srv/minecraft"}
: ${minecraft_path="/srv/minecraft/minecraft_server.jar"}
: ${minecraft_flags=""}
: ${minecraft_args="/usr/local/bin/java -Xmx1024M -Xms1024M \
                    -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing \
                    -XX:ParallelGCThreads=4 -XX:+AggressiveOpts \
                    -jar ${minecraft_path} ${minecraft_flags} nogui"}

minecraft_start() {
    unset "${rc_arg}_cmd"
    minecraft_flags="-d -m -S ${minecraft_session} ${minecraft_args}"
    if minecraft_running; then
        echo "minecraft already running?"
    else
        run_rc_command "start"
    fi
}

minecraft_stop() {
    local cmd
    cmd="${command} -p 0 -S ${minecraft_session} -X eval 'stuff stop\015'"
    if minecraft_running; then
        echo "Stopping minecraft."
        su -m ${minecraft_user} -c "${cmd}"
    fi
}

minecraft_status() {
    if minecraft_running; then
        echo "minecraft is running."
    else
        echo "minecraft is not running."
    fi
}

minecraft_running() {
    local check ses
    ses="${minecraft_session}"
    check=`su -m ${minecraft_user} -c "${command} -list" | grep ${ses}`
    if [ "$check" ]; then
        return 0
    else
        return 1
    fi
}

run_rc_command "$1"
Advertisement