Friday, December 16, 2011

Mineserv: Automatic Start/Stop Perl Init Script

I was getting tired of having to manually start and stop my Minecraft server whenever the server rebooted and I couldn't make backups without manually disabling level saving first. I wasn't too happy with the shell script alternatives so I wrote my own version with Perl.

Because the Minecraft server does not support daemon processes you need a program called "GNU Screen" which creates a virtual terminal that can run in the background and be re-attached when needed.

To install screen, in Debian based operating systems, run:
`sudo apt-get install screen`
For other distributions check your package manager.
Source can be found here.

This script should be very easy to modify and add functionality to and would work nicely for other services that require screen to run with a little bit of modification.
Let me know if you add any functionality or re-hash the script for something else and I can post it here. Please share alike.

Download: mineserv

Installing the Script
- Install "screen" if you don't already have it
- Edit the SETTINGS variables to fit your server environment.
- Make a copy of the file in /etc/init.d/
- Make it executable: sudo chmod a+x /etc/init.d/mineserv
- Add it to the init list: sudo update-rc.d mineserv defaults

Uninstalling the Script
- Simply delete the file: sudo rm /etc/init.d/mineserv
- Remove from the init list: update-rc.d -f mineserv defaults

Using the Script
- Usage: /etc/init.d/mineserv {start|stop|restart|status|backup|cleanup [days]}
(Backup makes a .tar.gz archive of the world folder in the backup directory
(Cleanup deletes backups older than [days] given in the backup directory)

- To send a command to the server console: /etc/init.d/mineserv command "your command"
- To bring up the server console run: screen -r mineserv (Make sure run as the correct user)
-To give items to players: mineserv give [player] [item id] [amount]
This command will allow you to give more than 64 items at a time so specify any amount!


Steve said...

You are a genius, thank you so much. Got this working with no trouble at all

Steve said...

The only issue I seem to have is that when Minecraft crashes, screen terminates and the server doesn't restart. And it's crashing every 5/10 minutes so I would need to manually restart the server each time.

Chris "BlackCow" Handwerker said...


Your server shouldn't be crashing that much to begin with!

However the script provides a "status" command to see if the server is running.

I'm sure you could make a cronjob to check the status of the server every 10 min or so.

The only reason I didn't add this functionality is if my server crashes for whatever reason it usually doesn't end the process but it rather just hangs up so perhaps I can also add functionality to restart after it hangs up too.

Anyway, glad someone made use of my script :D

Alexander James said...

When I start the script I keep getting told to enter the password for user and it quits....

Alexander James said...
This comment has been removed by the author.
Chris "BlackCow" Handwerker said...

@Alexander James

So the script is saying, "enter the password for user" and then it should specify what user depending on what you entered in for the settings in the script.

So if its not specifying which user it sounds like you didn't change the settings perhaps?

It could be that you are not running the script under the right user or the script doesn't have the correct permissions.

Normally the script is supposed to determine what user you are trying to run the script as and then change

Horst Nikolay said...

It works from beginning.You have done a very good job. Thanks for sharing.

shing said...

Love this script. Been using it for a couple of years with no problems at all.
I do have one question now that I've upgraded to a bit more ram on the server.
How do I increase ram usage? I'm running it on a purpose built ubuntu server
and I have a whole extra 7gigs free by the looks of things.