世界边境修剪的脚本服务器的Minecraft [英] World Border Trim Script for Minecraft Server

查看:119
本文介绍了世界边境修剪的脚本服务器的Minecraft的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个修剪服务器的Minecraft每天世界早上5点的脚本。到目前为止,它运行命令/ WB $ NAME修剪,$ name是世界的名字,然后运行命令/ WB微调确认,初始化微调过程。

I am trying to create a script that trims the worlds of a minecraft server every day at 5 AM. So far, it runs the command /wb $NAME trim, where $NAME is the name of the world, and then it runs the command /wb trim confirm, to initialize the trimming process.

#!/bin/bash
# /usr/local/bin/trim
# Title: World Border Trim Automator
# Author: Jonathan Bondhus

######### CONFIG STARTS HERE #########

# Location of the init script
INIT_SCRIPT="/etc/init.d/minecraft"

# Name to use for the screen instance
SCREEN="minecraft"

# User that should run the server
USERNAME="minecraft"

# Path to minecraft server directory 
MCPATH="/home/${USERNAME}/minecraft"

# Where the worlds are located on the disk
WORLDSTORAGE="${MCPATH}/worlds"

######### CONFIG ENDS HERE #########

## Start of script, don't edit anything below this line unless you know what you are doing

as_user() {
    if [ $ME == $USERNAME ] ; then
        bash -c "$1"
    else
        su $USERNAME -s /bin/bash -c "$1"
    fi
}

my_trim() {
    a=1
    for NAME in $(ls $WORLDSTORAGE)
    do
        if [ -d $WORLDSTORAGE/$NAME ]
        then
            WORLDNAME[$a]=$NAME
            a=$a+1
            # Run the /wb trim command
            echo "Running /wb $NAME trim..."
            as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"wb $NAME trim\"\015'"
            sleep 2     # Wait 2 seconds
            echo "Running /wb trim confirm..."
            as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"wb trim confirm\"\015'"
            echo "Waiting 10 minutes for trim to complete"
            sleep 600   # Wait 10 minutes (600 seconds)
        fi
    done
}

my_is_running(){
    # Checks for the minecraft servers screen session
    # returns true if it exists.
    if ps ax | grep -v grep | grep "$SCREEN $INVOCATION" > /dev/null
    then
        return 0
    fi
    return 1
}

my_main(){
    ME=`whoami`     # Sets $ME to equal the current user's username
    my_is_running
    if my_is_running
        then
            my_trim
        else
            echo "Server is not running... Starting..."
            my_as_user "$INIT_SCRIPT start"
            wait 100
    fi
}

my_as_user() {
    if [ $me == $username ] ; then
        bash -c "$1"
    else
        su $USERNAME -s /bin/bash -c "$1"
    fi
}

my_main
exit 0

有一个问题。如果它试图重新运行它,而微调的过程仍在进行,则服务器将忽略该请求,脚本会继续运行,仿佛一切都没有了问题。我希望它做的是检查日志,直到找到修​​剪完毕。整个修剪日志文件将是这个样子:

There is one problem. If it tries running it again while the trimming process is still taking place, then the server will ignore that request, and the script will keep running as if nothing has gone wrong. What I want it to do, is check the log until it finds that the trim is completed. The log file throughout a trim will look something like this:

2012-08-12 03:58:08 [INFO] World trimming task is ready for world "world", trimming the map past 208 blocks beyond the border (default 208), and the task will try to process up to 5000 chunks per second (default 5000).
2012-08-12 03:58:08 [INFO] This process can take a while depending on the world's overall size. Also, depending on the chunk processing rate, players may experience lag for the duration.
2012-08-12 03:58:08 [INFO] You should now use wb trim confirm to start the process.
2012-08-12 03:58:08 [INFO] You can cancel at any time with wb trim cancel, or pause/unpause with wb trim pause.
2012-08-12 03:58:10 [INFO] WorldBorder map trimming task started.
2012-08-12 03:58:35 [INFO] [WorldBorder] [Trim] 0 entire region(s) and 216 individual chunk(s) trimmed so far (11.8% done)
2012-08-12 04:00:04 [INFO] [WorldBorder] [Trim] 0 entire region(s) and 1016 individual chunk(s) trimmed so far (25.5% done)
2012-08-12 04:01:55 [INFO] [WorldBorder] [Trim] 0 entire region(s) and 1975 individual chunk(s) trimmed so far (36.6% done)
2012-08-12 04:03:18 [INFO] [WorldBorder] [Trim] 0 entire region(s) and 2718 individual chunk(s) trimmed so far (45.2% done)
2012-08-12 04:03:58 [INFO] [WorldBorder] [Trim] 0 entire region(s) and 3084 individual chunk(s) trimmed so far (52.7% done)
2012-08-12 04:04:12 [INFO] [WorldBorder] [Trim] 0 entire region(s) and 3203 individual chunk(s) trimmed so far (61.1% done)
2012-08-12 04:05:07 [INFO] [WorldBorder] [Trim] 0 entire region(s) and 3690 individual chunk(s) trimmed so far (73.5% done)
2012-08-12 04:05:53 [INFO] [WorldBorder] [Trim] 0 entire region(s) and 4080 individual chunk(s) trimmed so far (100.0% done)
2012-08-12 04:05:53 [INFO] [WorldBorder] [Trim] task successfully completed!

当然,日志并不只包含世界边界信息,它也包含人的聊天记录,以及记录一切发生。因此,我不能只是尾部的最后10行日志,并检查,因为一旦10日线已经过去了(例如,打印超过10行的间隔脚本检查最后10行之前等待一个错误) ,它不会永远将能够​​找到任何会导致它认识到它完成了,所以它会永远等待,使用了宝贵的资源和$ p $被修剪pventing世界,直到我发现,什么是错的,这可能需要几天,甚至几周,因为它的自动化。

Of course, the log doesn't only contain world border information, it also contains people's chats, as well as logs everything else that occurs. Therefore, I can't just tail the last 10 lines of the log and check that, as once 10 lines had passed (for example an error that prints more than 10 lines in the interval that the script waits before checking the last 10 lines), it's not going to ever going to be able to find anything that would cause it to realize that it finished, so it would wait forever, using up valuable resources and preventing the worlds from being trimmed until I noticed that something is wrong, which could take days, or even weeks to since it's automated.

它不仅将不得不停止完毕时,只适用于世界,但它会记录小时和分钟,当它开始修剪世界,之后只检查,因为如果我修剪同一个世界早些时候,否则就会导致问题,因为应该已经任务圆满完成的日志中世界,这将导致脚本几乎立即停止。有谁看到解决这个?这使我的头不疼想到它...:P

Not only would it have to stop when it is completed, only for that world, but it will have to record the hour and minute when it started trimming the world, and only check after that, because if I trimmed the same world earlier it would cause problems otherwise, as there would already be the "task successfully completed" in the log for that world, which would cause the script to stop almost immediately. Does anyone see a solution to this? This makes my head hurt just thinking about it... :P

推荐答案

你会发现任何优势,使用在脚本/功能正在运行.lock文件,并删除任何种类的完成(错误或以其他方式)?

Would you find any advantage to using a '.lock' file while the script/function is running, and delete it on ANY kind of completion (error or otherwise)?

锁定文件是0字节(空),用唯一的文件名称(通常是相同​​的名称作为文件名,以.lock末追加)。当运行脚本时,检查该文件是否存在。如果它不存在,创建它(即触摸$ MCPATH.lock ),然后运行命令修剪

The lock file is a 0-byte (empty) file with a unique name (usually the same name as the filename, with '.lock' appended at the end). When your script is run, check for the existence of that file. If it does not exist, create it (ie touch $MCPATH.lock) and then run your trim commands.

虽然这锁定文件的确实的存在,简单地等待()几秒钟​​(或更长的时间,因为你愿意的话),然后再次轮询

While that lock file does exist, simply wait() for a few seconds (or longer, as you wish) and then poll again.

一旦微调功能完成(即使出错完成),删除 .lock 文件。然后简单处理错误(或成功),你通常会。

Once the trim function completes (even if it completes with an error), delete that .lock file. Then simply handle the error (or success) as you would normally.

这篇关于世界边境修剪的脚本服务器的Minecraft的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆