Making Minecraft Linux server
Moderator: Emmz
- red_ned
- CiC-GoD
- Posts: 4757
- Joined: Sat Nov 24, 2007 2:23 am
- DayZ Name: Hobn0b
- Contact:
Making Minecraft Linux server
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
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
- red_ned
- CiC-GoD
- Posts: 4757
- Joined: Sat Nov 24, 2007 2:23 am
- DayZ Name: Hobn0b
- Contact:
Re: Making Minecraft Linux server
start.sh -> located in the /home/<user> folder
All the echo commands just allow you to see things are working if you run the commands by hand
1st part of script generates a string containing the date and time which can then be used at any point in the script using
This means lines give times while running
this line deletes the server file after changing into the correct folder
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
Now we start the server using a screen called minecraft - you can see about memory allocation from the minecraft wiki
next is stop.sh
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"
Code: Select all
./start.sh
Code: Select all
$NOW
this line deletes the server file after changing into the correct folder
Code: Select all
cd /home/cic/minecraft
rm minecraft_server.jar
Code: Select all
wget https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar
sleep 5
Code: Select all
screen -A -m -d -S minecraft java -Xms1024M -Xmx4096M -jar minecraft_server.jar nogui
- red_ned
- CiC-GoD
- Posts: 4757
- Joined: Sat Nov 24, 2007 2:23 am
- DayZ Name: Hobn0b
- Contact:
Re: Making Minecraft Linux server
stop.sh -> located in the minecraft folder
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
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
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
next is bounce.sh
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"
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")
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)"
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)"
- red_ned
- CiC-GoD
- Posts: 4757
- Joined: Sat Nov 24, 2007 2:23 am
- DayZ Name: Hobn0b
- Contact:
Re: Making Minecraft Linux server
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:
pretty easy this one
next back minecraft.sh for daily backups
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
next back minecraft.sh for daily backups
- red_ned
- CiC-GoD
- Posts: 4757
- Joined: Sat Nov 24, 2007 2:23 am
- DayZ Name: Hobn0b
- Contact:
Re: Making Minecraft Linux server
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
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
next just compress up whole folder (tar), add date to filename and move to correct folder
next up minecraft_chech.sh
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"
Code: Select all
find /home/cic/minecraft_backups/* -mtime +3 -exec rm {} \;
Code: Select all
tar -cjvf /home/cic/minecraft_backups/`date +%d-%m-%Y`_minecraft.tbz /home/cic/minecraft/
- red_ned
- CiC-GoD
- Posts: 4757
- Joined: Sat Nov 24, 2007 2:23 am
- DayZ Name: Hobn0b
- Contact:
Re: Making Minecraft Linux server
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).
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
so this says list and look for a screen called "minecraft"
the "if" statement then allows for
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
the logfile looks like this
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
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
Code: Select all
if ! screen -list | grep -q "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
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
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
to run this script every 5 mins next post is the crontabs
- red_ned
- CiC-GoD
- Posts: 4757
- Joined: Sat Nov 24, 2007 2:23 am
- DayZ Name: Hobn0b
- Contact:
Re: Making Minecraft Linux server
Crontabs - like windows scheduled tasks but much more powerful
mine look like this:
This line runs at 00:01 each day and just clears the screen.txt file and the mail file
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)
Ok so server is now down so next run the backup script minecraft.sh to make the tar files
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
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
i could do it like this instead but its quite long
etc... through each other hour except 06 to keep the script from running
read about crontabs here: http://en.wikipedia.org/wiki/Crontab
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
Code: Select all
01 00 * * * /home/cic/mail_clear.sh
Code: Select all
16 06 * * * /home/cic/minecraft/stop.sh
Code: Select all
17 06 * * * /home/cic/minecraft.sh
Code: Select all
19 06 * * * /home/cic/minecraft/bounce.sh
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
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
read about crontabs here: http://en.wikipedia.org/wiki/Crontab
- red_ned
- CiC-GoD
- Posts: 4757
- Joined: Sat Nov 24, 2007 2:23 am
- DayZ Name: Hobn0b
- Contact:
Re: Making Minecraft Linux server
ok i changed my check script timer to make sure the time around 6:15 is kept clear for backups and restart
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
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
- red_ned
- CiC-GoD
- Posts: 4757
- Joined: Sat Nov 24, 2007 2:23 am
- DayZ Name: Hobn0b
- Contact:
Re: Making Minecraft Linux server
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
- Smock
- 0ld Sk00L
- Posts: 264
- Joined: Sat Feb 23, 2008 9:18 pm
Re: Making Minecraft Linux server
u still using screen to run tasks in the background ned?



Visit my Steam Profile - [url=steam://friends/add/76561197960935400]Add me to your Steam friends[/url]