Tested in Debian 9 and CentOS7
It is assumed that you already have a user named minecraft and have the minecraft server setup and working in the users home directory.
We need to create a service file for game service that we would like to control
Once done you can control the service easily from the terminal, you can also set it up to allow specific users to control the services without any su or root intervention.
Log in as root or issue the commands as su
first lets create the minecraft services file.
[Unit] Description=Minecraft Java Server Documentation= Wants=network.target After=network.target [Service] User=minecraft Nice=5 KillMode=control-group SuccessExitStatus=0 1 Restart=always RestartSec=10 NoNewPrivileges=true PrivateTmp=true InaccessibleDirectories=/root /sys /srv -/opt /media -/lost+found WorkingDirectory=/home/minecraft/minecraftserver/ ExecStart=/usr/bin/java -Xms2G -Xmx6G -jar minecraftserver.jar --noconsole nogui [Install] WantedBy=multi-user.target
The only things you really need to change are the description
The user who will be running the server, in this case the user is “minecraft”
The working directory for the minecraft jar file
The java arguments that you use to start your minecraft server, this must also include the full path to your systems java
Save and exit vi :wq
Now we need to reload the system services so that your service is registered.
You can now control your minecraft server with the following commands
systemctl start minecraft
systemctl stop minecraft
systemctl restart minecraft
If you need to access your server to issue op commands from the terminal then don’t forget to stop the service, and load the server manually as you can’t interact with it from the terminal once the services is running.