Making Minecraft Linux server

Help with Game Server Configs + Mods of all types

Moderator: Emmz

User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

Making Minecraft Linux server

Post by red_ned »

Thought i would publish my scripts etc for minecraft running on Linux (opensuse in my case)

1. Go to minecraft.net > server file is https://s3.amazonaws.com/MinecraftDownl ... server.jar but you can click through the downloads area.

2. Copy minecraft_server.jar into a folder (i called mine "minecraft")

3. Make sure the file has execute permissions. chmod help is at -> http://en.wikipedia.org/wiki/Chmod

I use 3 main scripts to control the server.
bounce.sh -> located in the minecraft folder
start.sh -> located in the minecraft folder
stop.sh -> located in the minecraft folder

I also run a script to backup the server each day.
minecraft.sh -> located in the /home/<user> folder

I also run a "check server is running script every 5 minutes
minecraft_chech.sh -> located in the /home/<user> folder

I also currently have some log files to show scripts have run
screen.txt -> located in the /home/<user> folder
also the mail file in /var/spool/mail named as the user the scripts run under
and these 2 log files get cleaned each day to stop them getting too large
mail_clear.sh -> located in the /home/<user> folder
N.B. - <user> in this case is called cic but you should change to whatever you called your user account
Scripts up next
Image
May we all fight to live another day.
User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

Re: Making Minecraft Linux server

Post by red_ned »

start.sh -> located in the /home/<user> folder

Code: Select all

#!/bin/sh
NOW=$(date +"%Y-%m-%d %H:%M")
echo "$NOW getting Minecraft jar"
cd /home/cic/minecraft
rm minecraft_server.jar
wget https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
sleep 5
echo "$NOW got Minecraft jar"
echo "$NOW Starting Minecraft"
sleep 1
screen -A -m -d -S minecraft java -Xms1024M -Xmx4096M -jar minecraft_server.jar nogui
echo "$NOW Started Minecraft"
All the echo commands just allow you to see things are working if you run the commands by hand

Code: Select all

./start.sh
1st part of script generates a string containing the date and time which can then be used at any point in the script using

Code: Select all

$NOW
This means lines give times while running

this line deletes the server file after changing into the correct folder

Code: Select all

cd /home/cic/minecraft
rm minecraft_server.jar
Then we get a new copy of the server file (this doesnt currently check if server file is newer than current one) and pause for 5 seconds

Code: Select all

wget https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
sleep 5
Now we start the server using a screen called minecraft - you can see about memory allocation from the minecraft wiki

Code: Select all

screen -A -m -d -S minecraft java -Xms1024M -Xmx4096M -jar minecraft_server.jar nogui
next is stop.sh
Image
May we all fight to live another day.
User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

Re: Making Minecraft Linux server

Post by red_ned »

stop.sh -> located in the minecraft folder

Code: Select all

#!/bin/sh
NOW=$(date +"%Y-%m-%d %H:%M")
echo "$NOW Stopping Minecraft"
sleep 1
screen -S minecraft -p 0 -X stuff "say server going down for backup in 30 seconds$(printf \\r)"
sleep 5
screen -S minecraft -p 0 -X stuff "say server will now auto save$(printf \\r)"
sleep 5
screen -S minecraft -p 0 -X stuff "save-all$(printf \\r)"
sleep 10
screen -S minecraft -p 0 -X stuff "say server going down: please come back soon$(printf \\r)"
sleep 10
screen -S minecraft -p 0 -X stuff "stop$(printf \\r)"
echo "$NOW Stopped Minecraft"
This is a rather sophisticated script as instead of closing the screen down, which may mean the server hasn't saved recently and could corrupt quite easily so instead i have sent commands and messages into the minecraft server console instead.

Again i always incude date and time commands incase i want to use them at any point

Code: Select all

NOW=$(date +"%Y-%m-%d %H:%M")
and i also echo to users if they are doing things manually.

the next is to send in messages to any players on the server and wait some periods of time between each to give them time to leave

Code: Select all

screen -S minecraft -p 0 -X stuff "say server going down for backup in 30 seconds$(printf \\r)"
that just sends the message "say server going down for backup in 30 seconds" into the server

then i use the stop command inside the server at the end

Code: Select all

screen -S minecraft -p 0 -X stuff "stop$(printf \\r)"
next is bounce.sh
Image
May we all fight to live another day.
User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

Re: Making Minecraft Linux server

Post by red_ned »

bounce.sh -> located in the minecraft folder

This is just a way of running stop.sh then start.sh but is a much safer way of restarting as it makes sure you dont open multiple instances of the server:

Code: Select all

#!/bin/sh
cd /home/cic/minecraft
./stop.sh
sleep 1
./start.sh
pretty easy this one

next back minecraft.sh for daily backups
Image
May we all fight to live another day.
User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

Re: Making Minecraft Linux server

Post by red_ned »

minecraft.sh -> located in the /home/<user> folder
This makes a daily back up of the server, compresses, moves to folder "minecraft_backups" and deletes any backups older than 5 days

Code: Select all

#!/bin/sh
echo "deleting backups older than 4 days"
# Using find command
find /home/cic/minecraft_backups/* -mtime +3 -exec rm {} \; 
sleep 1
# Making tar file in destination folder
echo "compressing backup"
tar -cjvf /home/cic/minecraft_backups/`date +%d-%m-%Y`_minecraft.tbz /home/cic/minecraft/
sleep 1
# Finishing & giving message
echo "file backed up to minecraft_backups"
The file age line is a little odd as it has 5 files in the folder at any time but the script says 3 days as this looks for files older than 3 days and of course that's 4 files (ok the script runs at same time each day and checks for over 3 days a few minutes before new file is created so sees them as less than 4 days old) plus today's file

Code: Select all

find /home/cic/minecraft_backups/* -mtime +3 -exec rm {} \; 
next just compress up whole folder (tar), add date to filename and move to correct folder

Code: Select all

tar -cjvf /home/cic/minecraft_backups/`date +%d-%m-%Y`_minecraft.tbz /home/cic/minecraft/
next up minecraft_chech.sh
Image
May we all fight to live another day.
User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

Re: Making Minecraft Linux server

Post by red_ned »

minecraft_chech.sh -> located in the /home/<user> folder
this is a file to check the server screen is online, if it is it just logs into screen.txt but if screen is down it will restart the server (including an update).

Code: Select all

#!/bin/sh
NOW=$(date +"%Y-%m-%d %H:%M")

echo "$NOW Checking Minecraft" >> /home/cic/screen.txt
if ! screen -list | grep -q "minecraft"; then
cd "/home/cic/minecraft" && ./start.sh && echo "$NOW Minecraft Restarted" >> /home/cic/screen.txt

else 
echo "$NOW Minecraft Running" >> /home/cic/screen.txt

fi
The important line is the one which asks what screens are open and what they are called and find a specific name in the list

Code: Select all

if ! screen -list | grep -q "minecraft"; 
so this says list and look for a screen called "minecraft"

the "if" statement then allows for

Code: Select all

; then
cd "/home/cic/minecraft" && ./start.sh && echo "$NOW Minecraft Restarted" >> /home/cic/screen.txt
so it goes into the minecraft folder, runs the start.sh script and logs the restart

if it does find the screen it just logs and exists the script

Code: Select all

else 
echo "$NOW Minecraft Running" >> /home/cic/screen.txt
the logfile looks like this

Code: Select all

2013-05-15 22:45 Checking Minecraft
2013-05-15 22:45 Minecraft Running
2013-05-15 22:50 Checking Minecraft
2013-05-15 22:50 Minecraft Restarted
2013-05-15 22:55 Checking Minecraft
2013-05-15 22:55 Minecraft Running
I stopped the server to check it so you can see it was checked and found up twice and down once

to run this script every 5 mins next post is the crontabs
Image
May we all fight to live another day.
User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

Re: Making Minecraft Linux server

Post by red_ned »

Crontabs - like windows scheduled tasks but much more powerful

mine look like this:

Code: Select all

01 00 * * * /home/cic/mail_clear.sh
16 06 * * * /home/cic/minecraft/stop.sh
17 06 * * * /home/cic/minecraft.sh
19 06 * * * /home/cic/minecraft/bounce.sh
*/5 * * * * /home/cic/minecraft_check.sh
This line runs at 00:01 each day and just clears the screen.txt file and the mail file

Code: Select all

01 00 * * * /home/cic/mail_clear.sh
This line runs the stop.sh script each day at 6:16am (don't ask why i chose that time i just did as i remembered the last line could interfere with my scripts so keep reading)

Code: Select all

16 06 * * * /home/cic/minecraft/stop.sh
Ok so server is now down so next run the backup script minecraft.sh to make the tar files

Code: Select all

17 06 * * * /home/cic/minecraft.sh
lastly run the bounce script - ok it does shutdown commands which dont do anything as its all down but i always prefer to run to make sure its down and then started, also this does the daily download of the server file

Code: Select all

19 06 * * * /home/cic/minecraft/bounce.sh
i do hope the scripts all run in time and there are some delays to try to make sure things run in sequence - and the timing reasons?
Well the 5 minute checks could restart the server during backup or download

the check is run like this at the moment

Code: Select all

*/5 * * * * /home/cic/minecraft_check.sh
i could do it like this instead but its quite long

Code: Select all

5,10,25,30,35,40,45,50,55,00 * * * * /home/cic/minecraft_check.sh
15,20 00 * * * /home/cic/minecraft_check.sh
15,20 01 * * * /home/cic/minecraft_check.sh
15,20 02 * * * /home/cic/minecraft_check.sh
15,20 03 * * * /home/cic/minecraft_check.sh
15,20 04 * * * /home/cic/minecraft_check.sh
15,20 05 * * * /home/cic/minecraft_check.sh
15,20 07 * * * /home/cic/minecraft_check.sh
15,20 08 * * * /home/cic/minecraft_check.sh
15,20 09 * * * /home/cic/minecraft_check.sh
15,20 10 * * * /home/cic/minecraft_check.sh
etc... through each other hour except 06 to keep the script from running

read about crontabs here: http://en.wikipedia.org/wiki/Crontab
Image
May we all fight to live another day.
User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

Re: Making Minecraft Linux server

Post by red_ned »

ok i changed my check script timer to make sure the time around 6:15 is kept clear for backups and restart

Code: Select all

5,10,25,30,35,40,45,50,55,00 * * * * /home/cic/minecraft_check.sh
15,20 00 * * * /home/cic/minecraft_check.sh
15,20 01 * * * /home/cic/minecraft_check.sh
15,20 02 * * * /home/cic/minecraft_check.sh
15,20 03 * * * /home/cic/minecraft_check.sh
15,20 04 * * * /home/cic/minecraft_check.sh
15,20 05 * * * /home/cic/minecraft_check.sh
15,20 07 * * * /home/cic/minecraft_check.sh
15,20 08 * * * /home/cic/minecraft_check.sh
15,20 09 * * * /home/cic/minecraft_check.sh
15,20 10 * * * /home/cic/minecraft_check.sh
15,20 11 * * * /home/cic/minecraft_check.sh
15,20 12 * * * /home/cic/minecraft_check.sh
15,20 13 * * * /home/cic/minecraft_check.sh
15,20 14 * * * /home/cic/minecraft_check.sh
15,20 15 * * * /home/cic/minecraft_check.sh
15,20 16 * * * /home/cic/minecraft_check.sh
15,20 17 * * * /home/cic/minecraft_check.sh
15,20 18 * * * /home/cic/minecraft_check.sh
15,20 19 * * * /home/cic/minecraft_check.sh
15,20 20 * * * /home/cic/minecraft_check.sh
15,20 21 * * * /home/cic/minecraft_check.sh
15,20 22 * * * /home/cic/minecraft_check.sh
15,20 23 * * * /home/cic/minecraft_check.sh
it just has the 1st line making it run on certain minutes of every hour then as i missed out 15 and 20 past th hour i do it on every other hour except the 6:00 am time slot
Image
May we all fight to live another day.
User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

Re: Making Minecraft Linux server

Post by red_ned »

if you have questions or comments please ask away, plus i will be trying an if statement to determine if the download of the server file is worthwhile or not by comparing dates so if you have a guess on the code then please share it here
Image
May we all fight to live another day.
User avatar
Smock
0ld Sk00L
0ld Sk00L
Posts: 264
Joined: Sat Feb 23, 2008 9:18 pm

Re: Making Minecraft Linux server

Post by Smock »

u still using screen to run tasks in the background ned?
ImageImageImage
Visit my Steam Profile - [url=steam://friends/add/76561197960935400]Add me to your Steam friends[/url]
Post Reply