Content: Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Background: Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Pattern: Blank Waves Notes Sharp Wood Rockface Leather Honey Vertical Triangles
Welcome to TerraFirmaCraft Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

  • Announcements

    • Crysyn

      Only help if you can be helpful

      Hey All, A topic has come up of late in the IRC channel in regards to the general feel of the forums and the community that supports them. Things have progressed further than I would have liked with out this being addressed more publicly because I would much rather have snubbed this out sooner rather than later.. but I have been busy. Here is the general rule I would like people to follow: Wheaton's Law "Don't be a dick." Those of you from the IRC channel know that this is the only rule I ask people in there to follow and we generally have a good and lively time chatting about all manner of things. This is basic rule that just about everyone understands and I am going to expand it to the forums from here moving forward. If you can not help people in a helpful and polite manner then I simply ask you to stop. Now I generally take a back seat to moderating the forums as I like to participate in the suggestions forum fairly heavily at times and would rather do so as a forums user than a moderator. But I am also fairly well known for being the person who constantly puts their foot down and so I am stepping up and doing so on here. If you find yourself unable to respond to a message politely then I ask that you do not respond. This mostly focuses on the increasing level of hostility found within the Suggestion forum as well as the Server forum. I do not care if this is the 30th some odd time you have seen someone make the same suggestion. Or even if the new post on an older topic is one entry above the old one. I expect the members of this forum to respond politely to the user, new or old, and point to the older topic if it applies and even go the extra step to suggest they either add in new information or to summarize the outcome of the previous discussion based upon the new post's entry into it. That is what we are here for, that is why I close most topics instead of deleting them, so that they can be found and referenced down the road. The next topic is the slew of derailment attempts I have seen as of late. If you want to have fun and joke around that is what the off topic forum is for and pretty much anything goes there. I do not expect to read a suggestion thread and have to go through 3 pages of image memes people have shot back and forth. Quite simply this is a waste of my time to read and then have to clean up. Now for the summary. I am going to start taking a more active role, especially in policing the suggestion forum, and handing out warn levels to people whom I see doing this. These will be indiscriminate and applied not to just the first person who derails or is impolite on a topic or response, but to everyone whom follows the lead of that person. As I do not like doing things with out giving you all warning this post shall serve as that warning. If you have a desire to bring this topic up with me then I invite you to do so on the IRC channel. Lets raise the level of quality and grow the community. Let us not descend into the quality often found on the minecraft or league of legend forums. There is simply no need for that here. Be passionate about things, just do not be abusive.
    • Kittychanley

      Offline Servers

      Recently I've seen a few server listings showing up on the first page of the Servers forum that have been closed for an extended period of time, but have recently gotten a reply from a new member who didn't realize the server is offline. To help prevent this from happening in the future, it would be greatly appreciated if you could use the report function on the original post of any servers that have been confirmed as offline, so that the topic may be locked. If you are the admin of a server and plan on taking the server offline, please use the report function on the original post of your topic to let the TFC Staff know that the topic should be locked. If you are the admin of a server that has a locked topic, and would wish to bring the server back online, please use the report function on the original post of the topic to let the TFC Staff know that the topic should be unlocked. As always, please remember to follow rule #3 of the servers forum and update your topic title to contain the version of TFC that the server is currently running. You can do so by editing the OP, and then clicking on "Use Full Editor."
Uncle Gus

Automatically starting and stopping a linux server on player demand

4 posts in this topic

I found this pretty sweet blog post here and followed the instructions but it didn't work, due to the post being four years old and out of date. So I learned some Linux and re-engineered it. I tried to post a comment on it for anybody else who stumbled across it and wanted a working version, but the website doesn't seem to want my post. I figured other TFC server admins might appreciate having their servers automatically shut down and start up as players leave and join, so I thought I'd post my findings here for y'all. Here is what I was trying to post on the original blog:

 

 

Okay, I registered an account so that I could comment on this post. First of all, thank you for posting this! It was exactly what I was looking for. I don't care about server memory or anything like that, but I am running a Terrafirmacraft server, which updates the world based on time lapse, so it's not enough just to have chunks unloaded from memory. It also has seasons, so I don't want to log off in the middle of summer and come back to a cold winter with all my crops dead.

These scripts didn't work straight off the bat; this post is four years old, so lots of things are out of date. That's why I am posting this. I have spent several hours reverse engineering these commands to find out what they do and re-engineering them with up-to-date equivalents, while learning Linux (!!!) along the way. Before reading this, all I knew how to do in Linux was log in and run a script to get Minecraft running. I didn't even know how to stop it! If I wanted to stop it, I had to reboot the whole server.

So here is my version, for anyone else wanting to do the same thing:

Overview
What we're going to do is:

  1. Set up a webservice to listen on the default Minecraft port of 25565 and run a script when it hears something
  2. Create a script that the webservice will invoke that will check if the server is running or not, and start it if it isn't
  3. Set up a timer job to run every X minutes and invoke another script
  4. Create a script that the timer job will run that will check if there are any players on the server, and if not, shut it down


Setting up the webservice
For this you need xinetd installed. Try running it from the command line; if it isn't there, it will tell you how to install it. Create a file /etc/xinetd.d/minecraft:

 

service minecraft
{
disable = no
type = UNLISTED
port = 25565
socket_type = stream
protocol = tcp
user = root
wait = no
server = /home/UncleGus/bukkit/start.sh
max_load = 20.0
}



Change the server folder to point to your minecraft directory.
What this does is listen on port 25565 and run start.sh when it hears something. I don't know if it runs every time it hears something, but the way I have set it up, it doesn't matter. To get this service running, you will need to restart the xinetd daemon. This will happen at a reboot, but if you want to restart it now, you can with:
$ sudo service xinetd restart

Creating the start script
In your minecraft folder, create a file called start.sh with:

 

#!/bin/bash
cd "$(dirname "$0")"

# set these variables to what they need to be
RUN="forge-1.7.10-10.13.4.1614-1.7.10-universal.jar"
USERNAME="root"
MINMEM="256M"
MAXMEM="1280M"

# this line checks if java is running
if ! pgrep -f "$RUN" > /dev/null ; then
# this line starts a new screen session called minecraft and runs the .jar file in it
screen - dmS minecraft sudo -u "$USERNAME" -- /usr/bin/java -XX:ParallelGCThreads=2 -Djava.net.preferIPv4Stack=true -Xmx$MAXMEM -Xms$MINMEM -jar "$RUN"



Note the changes here. I found that even when I ran java as my own user, the java processes still ran as root, so checking for java processes run by myself didn't trigger. Also, the screen session didn't seem to run in such a way that the stop script could talk to it unless I used root here. I'm a total Linux noob so I've probably done something wrong here and my server is totally insecure and you can probably hack in and have your way with my tiny azure server running nothing but minecraft. Knock yourself out. I also removed everything to do with running nc and forwarding ports; more about that later. Also, those echo statements didn't work and weren't necessary.

Setting up the timer job
Linux has a list of timer jobs in a table (basically just a file) called crontab. If you type:
$ crontab -e
You will be able to edit this table of jobs. The way it works is you define the time pattern and the command as follows:
MIN HOUR DAYOFMONTH MONTH DAYOFWEEK COMMAND
So something like "15 4 12 3 * echo Hi" would output "Hi" at 4:15am on the 12th of March. There are certain commands for repeating jobs etc. So add:
*/1 * * * * sudo /home/UncleGus/bukkit/stop.sh
to the bottom, changing the directory to where your minecraft server is located. */1 means every minute. This will take effect the next time you restart your server. Note the sudo. Probably very bad practice, but I found that without it, the process could not find the screen session.

Creating the timer job script to stop the server
In your minecraft folder, create a file called stop.sh with:


 

#!/bin/bash

cd $(dirname $0)
scan=minecraft
pl() {
screen -S $scan -p 0 -X stuff '/list\n'
echo $(tail -n 1 /home/UncleGus/bukkit/logs/latest.log | wc -m)
}

[ $(pl) -lt 35 ] && {
echo "No players connected, waiting for three minutes"
sleep 180
[ $(pl) -lt 35 ] && {
echo "Still no players connected, shutting down"
screen -S $scan -p 0 -X stuff '/stop\n'
}
}


What this does in the pl() function is find the first screen session called minecraft and "stuff" a command into it of "/list" (in the original, the command was "who\015" but the who command doesn't work and the \0 was misplaced. \015 is the same as \n, the newline character).
It then counts the number of characters on the last line of the server log (note the different location for the log file now). If there are no players, the count will be 34. If there are players, the count will be higher. So this script runs pl() and if there are less than 35 characters, it will wait for three minutes (in case people haven't had a chance to join yet) and then check again and send the "/stop" command if there are still no players.

Don't forget to sudo chmod +x your script files so that they can actually be executed as scripts.

Regarding the port forwarding, nc seems to have changed since this article was written. I did manage to get the port forwarding working once using nc.traditional, but I decided against using it in the end. My reasoning:

  1. I don't know if the start.sh script is run every time the webservice hears something on port 25565. If it does, this could be a bit messy if players are connecting over 25565.
  2. I only got it working once.
  3. If you have 25565 forwarded to 25555 on the server, then every time someone opens their multiplayer screen and queries the server on 25565, it will wake it up, even if they aren't intending to join it. By leaving it separate, you can have players add the server to their list, using port 25555, and when they want to wake the server up, they can attempt to direct connect to the server address without specifying the port. It will fail, but the attempt will invoke the wake up routine. Then they can just refresh their server list until the 25555 server is up.

I hope someone else finds this helpful, I certainly learned a lot and am enjoying having my on-demand TFC server.

0

Share this post


Link to post
Share on other sites

I'm not sure why you'd want to do that. It is overkill. TFC slows down time when no players are on. At 1000x time is pretty much at a standstill. Then just have scheduled restarts with chat warnings.

0

Share this post


Link to post
Share on other sites
19 hours ago, Bunsan said:

TFC slows down time when no players are on.

.... it what now?

0

Share this post


Link to post
Share on other sites

Oh lol. Well, you learn something new every day. I guess I'll just change that setting and leave the server running.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now