Skip to content

SystemCTL & user update for MC Servers #14

@lx78WyY0J5

Description

@lx78WyY0J5

Add a user per server

Stop using screen & replace by full SystemCTL usage : https://www.youtube.com/watch?v=B6XcbvTiiG8

journalctl -fu <SERVICE> to get log data

test restart function inside spigot to match start system (Should point to start script)

Exemple

[Unit]
Description= MinecraftTest Service

[Service]
User=minecraft
Group=minecraft
WorkingDirectory=/opt/mc1
ExecStart=/usr/bin/java -Xms128M -Xmx6500M -jar /opt/mc1/server.jar nogui <REPLACE WITH START SCRIPT PATH LIKE PREVIOUS SYSTEMCTL CONF>
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Script path : Prev SystemCTL : https://github.com/Altherneum/server/blob/main/scripts/minecraft.service

Also move SystemCTL from /scripts to the real systemCTL path /etc/systemd/system/<NAME>.service

Nice tutorial for ref to add later on doc : https://www.youtube.com/watch?v=-C_lyhfUuhc

Add a socket for this service like shown here : https://unix.stackexchange.com/a/612118

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](https://gist.github.com/stolksdorf/5936212), 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.
[Share](https://unix.stackexchange.com/a/612118)
[Improve this answer](https://unix.stackexchange.com/posts/612118/edit)
Follow
[edited Feb 26, 2024 at 7:48](https://unix.stackexchange.com/posts/612118/revisions)
answered Sep 30, 2020 at 7:01
[Stewart](https://unix.stackexchange.com/users/272848/stewart)'s user avatar
Stewart

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementNew feature or request

    Type

    No fields configured for Task.

    Projects

    Status

    No status

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions