Tutorials Ubuntu startup script


Info

Warning
Since this is a wiki and anyone may modify any page at any time, it is suggested that you do not actually use this script but instead simply use it as a guideline for writing your own.

This is a simple startup-script for the upstart mechanism used by ubuntu-servers. It won`t give you a console, which I don`t consider a problem since there`s an ingame console anyway. It also doesn`t update Minecraft, I believe there`s a built-in updater coming on the way.

Note: It`s working fine in Ubuntu 14.

Further note: This configuration syntax is for the "upstart" process management system. Since 15.04, Ubuntu has moved to systemd, so you may need to explore other, systemd based options on newer Ubuntu installs. An example is below.

Prerequisites

  • set up a user and group for minecraft so that it doesn`t run as root
 sudo adduser --system --home /srv/minecraft-server minecraft sudo addgroup --system minecraft sudo adduser minecraft minecraft # this adds user "minecraft" the group "minecraft" 
  • hand your server-installation over to our new user, I have mine in /srv/minecraft-server
 sudo chown -R minecraft.minecraft /srv/minecraft-server 
  • create and edit the start/stop script creating a new file under /etc/init/minecraft-server (NOT /etc/init.d but /etc/init (without the .d) ) make sure that you save the file as minecraft-server.conf
 sudo nano /etc/init/minecraft-server.conf 

and pasting the following into it. Don`t forget to change the directory after "chdir" to your installation

 # description "start and stop the minecraft-server"  start on runlevel 2345 stop on runlevel ^2345  console log chdir YOUR_INSTALLATION_DIRECTORY setuid minecraft setgid minecraft  respawn respawn limit 20 5  exec /usr/bin/java -Xms1536M -Xmx2048M -jar minecraft_server.jar nogui 

Above Explained (Note, this script isn`t usable.):

 # description "start and stop the minecraft-server"  Executes command `/usr/bin/java` > Invokes Java. `-Xms1536M` > Sets minimum allocated memory limit. `-Xmx2048M` > Sets MAXIMUM allowable memory limit. `-jar minecraft-server.jar` > Tells Java to run the jarfile minecraft_server.jar `nogui` > Tells minecraft-server.jar to run without a GUI. (Graphical screen). 
  • Now install the start/stop to the default runlevels by running
 sudo initctl reload-configuration 

If you get an error saying the file does not exsist, try moving the minecraft-server file to /etc/init.d/ Also if after running sudo start minecraft-server says that they is no job for it, try running "sudo /etc/init.d/minecraft-server start"

Videos

Usage

  • to start the server
 sudo start minecraft-server 
  • to stop the server
 sudo stop minecraft-server 

Troubles?

You can check the server log by using:

 sudo tail /var/log/upstart/minecraft-server.log 

If that doesn`t help, just contact me: User:Cel1ne

With systemd

I have done a functional systemd script, if it can be useful...

I`ve created a user, as chown by Cel1ne.

After:

 sudo gedit  /etc/systemd/system/minecraft-server.service 

inside:

 Unit 
Description=start and stop the minecraft-server

Service
WorkingDirectory=/srv/minecraft-server
User=minecraft
Group=minecraft
Restart=on-failure
RestartSec=20 5
ExecStart=/usr/bin/java -Xms1536M -Xmx1536M -jar minecraft_server.jar nogui

Install
WantedBy=multi-user.target

To start the service:

 sudo service minecraft-server start 

To start after boot:

 sudo systemctl enable minecraft-server 

Voir le statut ("q" to go out of the view):

 systemctl status minecraft-server.service  

If you would like to interact with the server from your keyboard, you may use an alternative service configuration like this:

 Unit  Description=minecraft-server   Service WorkingDirectory=/home/minecraft/server User=minecraft Group=minecraft Type=forking ExecStart=/usr/bin/screen -h 2048 -dmS minecraft /usr/bin/java -Xms1536M -Xmx1536M -jar minecraft_server.jar nogui  Install WantedBy=multi-user.target 

If you have changed the service file, you should issue the

 sudo systemctl daemon-reload 

command to update the system.