I suspect you've stumbled on this blog post which uses screen
to solve a problem where your minecraft server stops when you $ java -jar spigot.jar
, then close your ssh or putty session. That method seems to have become the cannonical answer as to how to run a minecraft server, even though it isn't necessary.
systemd is a totally different (and better) solution to this problem, circumventing the need for screen
. You can achieve everything you've done in your script with systemd service options.
To run a vanilla minecraft server, create /etc/systemd/system/minecraft.service
with this content:
[Unit]
Description=Minecraft Server
[Service]
Type=simple
WorkingDirectory=/home/minecraft
ExecStart=java -Xmx1024M -Xms1024M -jar /home/minecraft/server.jar nogui
User=minecraft
Restart=on-failure
[Install]
WantedBy=multi-user.target
Set it to launch automatically after boot with systemctl enable minecraft
.
You asked about how to control it:
$ sudo systemctl start minecraft # Starts the service if it wasn't running
$ sudo systemctl stop minecraft # Stops the service
$ sudo systemctl restart minecraft # Restarts the service
$ sudo systemctl status minecraft # Find out how the service is doing
$ sudo journalctl -u minecraft -f # Monitor the logs
This does everything except give you a means to send commands to the console. To do that, we'll set up a file that the server will listen to where you can write your commands by creating the following systemd units:
/etc/systemd/system/minecraft.socket
:
[Unit]
PartOf=minecraft.service
[Socket]
ListenFIFO=%t/minecraft.stdin
and /etc/systemd/system/minecraft.service
:
[Unit]
Description=Minecraft Server
[Service]
Type=simple
WorkingDirectory=/home/minecraft
ExecStart=java -Xmx1024M -Xms1024M -jar /home/minecraft/server.jar nogui
User=minecraft
Restart=on-failure
Sockets=minecraft.socket
StandardInput=socket
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
Now you can send console commands by echoing stuff into that file:
echo "help" > /run/minecraft.stdin
echo "/stop" > /run/minecraft.stdin
What's also cool is that you can make your own custom sequences of commands and cat
the entire file into the console. For example, if you play UHC, you can start a new world, have people log-in, then cat uhc.commands > /run/minecraft.stdin
to set the gamerules, spread the players, and start the event.
systemd
s restart feature?java -jar spigot.jar
stops. And I also want to run the jar file in screen so I can reattach to it at anytime.journalctl
. Or do you need to interact with it?systemctl status spigot.service