教程/FreeBSD啟動腳本

出自Minecraft Wiki
跳到: 導覽搜尋
警告
因為這是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
  • 如果你以及擁有了伺服器配置文件、世界文件等,將它們複製到 /srv/minecraft ,並且將它們的所有權給予你的 minecraft 用戶。
  • 編輯 /etc/rc.conf.local (如果不存在就新建),並添加下列行:
minecraft_enable="yes"
  • 伺服器現在會開始啟動了。為了啟動伺服器,檢查狀態,然後關閉,使用下列指令:
# /usr/local/etc/rc.d/minecraft start
# /usr/local/etc/rc.d/minecraft status
# /usr/local/etc/rc.d/minecraft stop

或:

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

腳本[編輯 | 編輯原始碼]

#!/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"