Minecraft Wiki

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

了解更多

Minecraft Wiki
Advertisement
该页面的内容不代表Mojang StudiosMinecraft Wiki的官方意见。

Tectoncius 可以被编写成脚本运行在任何 Linux 或任何基于 BSD 的操作系统上,包括 mac OS X。这基于了系统管理员在不需要手动触发一些东西就能计划生成地图了。

要达成目的,需要2样东西:一个脚本和一个cron条目。

示例脚本

脚本是执行实际的事情来启动 Tectonicus 渲染的。它包含了 Tectonicus 命令和其他一些您想包含在内的东西。下面的示例脚本会触发 Tectonicus 并使用rsync将生成后的地图、每个操作的耗时和输出日志文件上传到一个 web 服务器。

这里是脚本:

# Script to run Tectonicus 2.02 on Minecraft map data and send results to web server
# 
# The script will terminate with the following result codes:
# 0 - success
# 3 - Tectonicus terminated with some kind of error
# 4 - rsync terminated with some kind of error
#
# Let's make some variables!
#
# Log file where we track the progress of this script:
LOG="/Users/You/Applications/minecraft-server/tectonicusnightly-log.txt"
#
# Previous run's log file:
LOGOLD="/Users/You/Applications/minecraft-server/tectonicusnightly-log.old"
#
# Location of map files:
WORLDWORKDIR="/Users/You/Applications/minecraft-server/world"
#
# Location of current Tectonicus build:
TECTJAR="/Users/You/Applications/minecraft-server/Tectonicus_v2.02.jar"
#
# Location of current Tectonicus config files:
TECTCONFIG="/Users/You/Applications/minecraft-server/tectonicus.xml"
#
# Places where Tectonicus writes its output files (set in tectonicus.xml file):
TECTOUTPUT="/Users/You/Applications/minecraft-server/tectonicus"
#
# FQDN of the web server where the finished map will live and be available:
WEBSERVER=whatever.something.com
#
# Service account for logging onto the web server:
WEBSVC=yourwebserveraccount
#
# Finished map's location on the web server:
WEBSERVERPATH=/var/www/tectonicus-map
#
#
#
#
# Storing the starting time in a variable, which we'll use to figure out how long the script runs
#
STARTTIME="$(date +%s)"
#
# Setting up a log file to watch over all of this:
if [ -f $LOG ]
then
	mv -f $LOG $LOGOLD
	echo "Previous log found and backed up to tectonicusnightly-log.old" >> $LOG
	echo "Starting run at $(date)" >> $LOG
else
	echo "No previous log found! Creating log..." >> $LOG
	touch $LOG
	echo "Starting run at $(date)" >> $LOG
fi
#
#
#
#
# Invoking Tectonicus and running it for the map, making sure it completes OK:
#
echo "$(date +"%T") - Now running Tectonicus to generate maps..." >> $LOG
java -jar $TECTJAR config=$TECTCONFIG
if [ "$?" -ne "0" ]; then
        echo "$(date +"%T") - Tectonicus maps failed to complete--check TectonicusLog.txt for details" >> $LOG
	echo "$(date +"%T") - Aborting" >> $LOG
        exit 3
else
        echo "$(date +"%T") - ...Tectonicus finished rendering maps" >> $LOG
fi
#
#
#
#
# Finally, we need to get the results up onto the web server.  Rsync is the best tool, and will make sure
# we don't have to transfer the entire set of map files every time.  Rather than running the rsync daemon
# on the server, we'll just push the rsync traffic through ssh.  You'll need to enable ssh key authentication,
# to make this work seamlessly. We also do this using the web service account so that the ownership of the
# files is correct on the web server.
#
echo "$(date +"%T") - Transferring new and changed map files to web server via rsync..." >> $LOG
rsync --delete --force -aPe ssh --exclude 'Cache' $TECTOUTPUT/* $WEBSVC@$WEBSERVER:$WEBSERVERPATH
if [ "$?" -ne "0" ]; then
        echo "$(date +"%T") - Error transferring map files -- aborting" >> $LOG
        exit 4
else
        echo "$(date +"%T") - ...rsync of map files to web server completed successfully" >> $LOG
fi
#
#
#
#
# Recording the time now in another variable, so we can measure the script's execution time.
#
STOPTIME="$(date +%s)"
ELAPSEDTIME="$(expr $STOPTIME - $STARTTIME)"
#
#
#
#
# We don't really have any temp files or anything to clean up, so we can do our time math and stop here.
#
echo "Nightly run completed successfully at $(date)" >> $LOG
# OS X date
echo "Total time elapsed: $(date -ur $ELAPSEDTIME +%H:%M:%S)" >> $LOG
# Linux date
#echo "Total time elapsed: $(date -u -d @$ELAPSEDTIME +%H:%M:%S)" >> "$LOG"
#
# All done!
exit 0

示例脚本分解

这可能看起来像一个吓人的文字块,特别是从未接触过脚本的人,因此让我们深入探究脚本的每个部分吧。

退出状态

最顶端的脚本列出了可能的退出状态,脚本报告状态后便会停止运行。

# The script will terminate with the following result codes:
# 0 - success
# 3 - Tectonicus terminated with some kind of error
# 4 - rsync terminated with some kind of error

退出状态为0时意味着没有错误需要报告,而其他数字表面 Tectonicus 自身出现了错误或在进行rsync时出错。这些退出状态数字可以被用来确定在遇到错误时下一步要干的事,例如您可以重新触发 Tectonicus ,如果脚本返回退出代码3。虽然这个脚本不会再干任何事情。

变量

示例脚本的下一部分就是设置变量为下面的脚本使用。您可以改变一些东西来做到这点(就像 Tectonicus jar 文件或工作文件的位置)而不需要编辑脚本的主要部分。

# Log file where we track the progress of this script:
LOG="/Users/You/Applications/minecraft-server/tectonicusnightly-log.txt"
#
# Previous run's log file:
LOGOLD="/Users/You/Applications/minecraft-server/tectonicusnightly-log.old"
#
# Location of map files:
WORLDWORKDIR="/Users/You/Applications/minecraft-server/world"
#
# Location of current Tectonicus build:
TECTJAR="/Users/You/Applications/minecraft-server/Tectonicus_v2.02.jar"
#
# Location of current Tectonicus config files:
TECTCONFIG="/Users/You/Applications/minecraft-server/tectonicus.xml"
#
# Places where Tectonicus writes its output files (set in tectonicus.xml file):
TECTOUTPUT="/Users/You/Applications/minecraft-server/tectonicus"
#
# FQDN of the web server where the finished map will live and be available:
WEBSERVER=whatever.something.com
#
# Service account for logging onto the web server:
WEBSVC=yourwebserveraccount
#
# Finished map's location on the web server:
WEBSERVERPATH=/var/www/tectonicus-map

您将会在每个变量填写您自己的信息。它们有:

  • LOG 变量是脚本写它自己的简单的日志文件的地方,只是一个简单的文本文档。日志文件记录了脚本的运行,以及失败的原因。更重要的是,它记录了脚本是如何运行的。
  • LOGOLD 是之前日志文件的名称,这个名称随时都会改变。指定 LOGOLD 将会让日志文件保持不变并且覆盖原有的日志文件。
  • WORLDWORKDIR 是 Minecraft 地图数据文件的位置。设置这个参数到您想 Minecraft 地图储存到您的电脑的地方。如果您正在其他服务器运行您的地图,请首先复制它到您的电脑上。
  • TECTJAR 是当前 Tectonicus jar 文件的位置。因为 Tectonicus 更新了,您将要改变这个变量到新的 jar 文件。
  • TECTCONFIG 是 Tectonicus XML 配置文件的位置。Tectonicus 可以拥有多个配置文件,并且会以不同的变量名称列出,然后多次触发 Tectonicus 查看不同的地图。
  • TECTOUTPUT 是 Tectonicus 放置生成地图的地方。这里是您将地图复制到 Web 服务器的地方,甚至是在 Internet 上的某个位置。
  • WEBSERVER 是 Tectonicus 使用rsync上传地图到指定服务器的完全限定域名。
  • WEBSVC 是使用rsync登录到 web 服务器的账号。注意这个过程必须没有漏洞并且是安全的,您将要在这个脚本中指定账号的密码。这里是如何建立基于 SSH 的身份验证。
  • WEBSERVERPATH 是 Tectonicus 要将上传地图文件到 web 服务器的地址,这个地址可以被替代,以便可以在 Internet 上提供服务。

日志

下一个段落就是设置其他变量了,其名为 STARTTIME,记录了脚本启动的时间:

STARTTIME="$(date +%s)"

时间以Unix时间为格式,一个以协调世界时1970年1月1日 00:00 以来经过的秒数表示时间格式。在脚本的末尾,Unix时间将会储存在其他变量中,将会记录运行这个脚本所使用的时间。只需要记录秒,这样使用数学方法计算时间就会方便许多——这条途径将不会遇到多次小时/分钟/秒的转换,因为脚本的工作时间已经使用秒。

下一步设置脚本的日志文件:

if [ -f $LOG ]
then
	mv -f $LOG $LOGOLD
	echo "Previous log found and backed up to tectonicusnightly-log.old" >> $LOG
	echo "Starting run at $(date)" >> $LOG
else
	echo "No previous log found! Creating log..." >> $LOG
	touch $LOG
	echo "Starting run at $(date)" >> $LOG

第一样要做的东西就是要检查指定的路径是否已经存在一个日志文件,如果存在,就必须要在 mv 命令上重命名它,并将值储存到 LOGOLD 变量中。之后,一个在备注会放置到日志文件中,表明了旧日志已经备份,并且记录了脚本开始运行的时间的描述。

如果日志文件不存在,脚本会新建一个,并记录启动时间。

运行 Tectonicus

下一个段落就是启动 Tectonicus 并且在继续操作之前等待地图构建完成。

echo "$(date +"%T") - Now running Tectonicus to generate maps..." >> $LOG
java -jar $TECTJAR config=$TECTCONFIG
if [ "$?" -ne "0" ]; then
        echo "$(date +"%T") - Tectonicus maps failed to complete--check TectonicusLog.txt for details" >> $LOG
	echo "$(date +"%T") - Aborting" >> $LOG
        exit 3
else
        echo "$(date +"%T") - ...Tectonicus finished rendering maps" >> $LOG

在日志文件中的第一行的备注就是 Tectonicus 实际启动时间,并且下一行就是启动 Tectonicus,使用一开始设置的变量指定 Tectonicus jar 和配置文件的位置。

下一部分,就是“IF”句子开始的地方,就是党它完成时抛出的 Java 退出状态。如果 Tectonicus 在顺利运行而没有报告错误,那么 Java 就会在完成时发送退出状态0。脚本监视着除0以外的退出状态(就是“if $? -ne 0”部分——如果退出代码不为0),并且如果监听到除0以外的任何东西,它会输出它自己的错误消息到日志文件和 terminates 中。

然而,如果 Java 的确返回了0退出代码——表明 Tectonicus 已成功运行——我们在这个部分里注意了太长的时间,也都是时候将目光转移到下一段了。

用 Rsync 传输地图

Rsync 是一个强大的多用途的文件传输工具,但是在这里,我们关心的是它从源中查找一大堆文件到目标位置的能力,以及从源中传输文件到目标位置的能力是不同的。Tectonicus 生存了许多许多文件,而每晚只传输有新字节的地图比传输整个地图能节省很多的时间。

echo "$(date +"%T") - Transferring new and changed map files to web server via rsync..." >> $LOG
rsync --delete --force -aPe ssh --exclude 'Cache' $TECTOUTPUT/* $WEBSVC@$WEBSERVER:$WEBSERVERPATH
if [ "$?" -ne "0" ]; then
        echo "$(date +"%T") - Error transferring map files -- aborting" >> $LOG
        exit 4
else
        echo "$(date +"%T") - ...rsync of map files to web server completed successfully" >> $LOG
fi

第一行记录了 Rsync 开始传输的时间,第二行就是运行 Rsync。这里是命令行的每个参数的作用:

  • --delete 移除在源中已被移除但在目标位置中还存在的文件,这能将杂乱程度保持在一个较低的水平
  • --force 保证在源中已被移除的文件的在目标位置中的对应文件已被移除,这再次将杂乱程度保持在一个较低的水平
  • -aPe 拥有三个选项,只能选择其中一个:
    • a 告诉 rsync 使用其“archive”预设,将您想复制的文件夹整个(包括其子目录)复制到服务器中,并且保护文件的布局和属性。
    • P 告诉 rsync 保持已传输的文件不变,并且在复制文件时显示进度显示器,因此当脚本在运行时,您会看见一些东西。
    • e ssh 告诉 rsync 与远程计算机建立 SSH 连接来执行命令。
  • --exclude 'Cache' 让 rsync 跳过复制 Tectonicus 的高速缓存目录。高速缓存目录可以非常大,拥有成千上万个的文件,因此不要将这个传输到服务器上,并且可以节省许多时间。
  • $TECTOUTPUT/* $WEBSVC@$WEBSERVER:$WEBSERVERPATH 告诉 rsync 复制 Tectonicus 输出目录到 web 服务器的目标位置中,并且使用您指定的账户登录。在一开始定义的变量的值会在脚本运行时被代替。

时间和完成

最后两段测量了实体脚本运行了多长时间,并将其记录到日志文件中,在报告运行成功后就会退出。

# Recording the time now in another variable, so we can measure the script's execution time.
#
STOPTIME="$(date +%s)"
ELAPSEDTIME="$(expr $STOPTIME - $STARTTIME)"
#
#
#
#
# We don't really have any temp files or anything to clean up, so we can do our time math and stop here.
#
echo "Nightly run completed successfully at $(date)" >> $LOG
# OS X date
echo "Total time elapsed: $(date -ur $ELAPSEDTIME +%H:%M:%S)" >> $LOG
# Linux date
#echo "Total time elapsed: $(date -u -d @$ELAPSEDTIME +%H:%M:%S)" >> "$LOG"
#
# All done!
exit 0

当前Unix时间会记录在 STOPTIME 变量,然后使用一些方便的数学运输,STARTTIME 减去 STOPTIME 就能得到脚本运行时间了。在将该值记录到日志文件之前,会先转换成小时/分钟/秒。(一些有限制的测试显示 date -ur 可在 OS X 上顺利工作,但在 Linux 上不会工作)。最后,我们报告0退出状态,脚本也结束了。

对于 Linux 机器,请尝试代替“Total time elapsed”行。

使用 Cron 让 Tectonicus 自动运行

要达成目的,最好的方法就是在 OS X 上使用wikipedia:Launchd安排一个计划,但是该部分教程都是针对 cron 写的,因为 cron 可以在 OS X、Linux 和其他基于 BSD 的操作系统上工作。为了说明如何在每天晚上定时执行一个脚本,请百度。

为了告诉 cron 在夜晚运行这个脚本,无论用户是否已经登录了,您都需要编辑系统 crontab 文件,该文件在 /etc/crontab (虽然在 OS X 10.6.x 可能找不到这个文件——如果真的是,您可以自己创建它)。编辑文件并粘贴下列内容:

0 1 * * * root <path to your script> &> /dev/null

确保路径中已经代替为 Tectonicus 的脚本路径。头2个字符是分钟和小时,也就是 cron 在24小时里定时运行任务的时间,因此这行表示在每晚的01:00执行脚本。为了改变时间为03:30,您可以将头2个字符改成“30 3”(不含双引号)。

总结

脚本只是自动运行 Tectonicus 的一种示例方式——它可以被其他东西作出大量的修改,包括运行多个 Tectonicus 实例,还有以串行端口或并行端口中工作。

这篇文章可能对如何使用 cron 创建一个计划任务的说明有用,但是我的 cron 工作很好,因此我没有花时间弄清楚如何让 launchd 进行工作。

Advertisement