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!