Sample Header Ad - 728x90

How should I set up a systemd service to auto-start a server and let me pass commands to it?

5 votes
1 answer
2336 views
#### Goal: I'm trying to get a Minecraft server to run on computer boot with systemd on Fedora. I have a few self-imposed criteria that I need to meet to be able to properly manage my server(s): 1. It has to run as the minecraft system user I made with the home dir /opt/minecraft. I attempted this one by addusering and then adding the line User=minecraft and WorkingDirectory=/opt/minecraft/ 2. It has to be scalable and work with an arbitrary number of servers. I attempted this by using a template service and then changing the WorkingDirectory line to WorkingDirectory=/opt/minecraft/%i to let me pass in a directory. 3. I have to be able to pass commands into it somehow. This is the one I'm stuck on. I have tried using a socket unit and then hooking that up to /run/minecraft%I, but I haven't been able to get that to work. If you aren't familiar with Minecraft servers, they have this interactive console thingy that you can pass commands into. In the past, I have used tmux send with the server running in a tmux session, but the issue with that is that it doesn't start automatically and feels inelegant. #### Attempted solution: /usr/local/lib/systemd/system/minecraft@.service:
[Unit]
Description=Minecraft server: %i

# only run after networking is ready
After=network-online.target
Wants=network-online.target

[Service]
Type=simple

# restart if the server crashes
Restart=on-failure
RestartSec=5s

# set the input and outputs to a socket unit and the journal resp.
Sockets=minecraft@%i.socket
StandardInput=socket                     
StandardOutput=journal
StandardError=journal

# set the user and directory to the correct values
User=minecraft
WorkingDirectory=/opt/minecraft/%i/

# run the start script for the specified server
ExecStart=/bin/bash /opt/minecraft/%i/start.sh

[Install]
WantedBy=default.target
/usr/local/lib/systemd/system/minecraft@.socket:
[Unit]
Description=Socket for Minecraft server: %i

[Socket]
# listen to a pipe for input
ListenFIFO=%t/minecraft%I.stdin

Service=minecraft@%i.service
#### Problem: When I try to start the server with sudo systemctl start minecraft@1_17_1.service (I have the server installed in /opt/minecraft/1_17_1/), it fails:
Job for minecraft@1_17_1.service failed because of unavailable resources or another system error.
See "systemctl status minecraft@1_17_1.service" and "journalctl -xeu minecraft@1_17_1.service" for details.
This prompted me to run systemctl status minecraft@1_17_1.service:
● minecraft@1_17_1.service - Minecraft server: 1_17_1
     Loaded: loaded (/usr/local/lib/systemd/system/minecraft@.service; enabled; vendor preset: disabled)
     Active: activating (auto-restart) (Result: resources) since Thu 2021-11-04 14:37:27 EDT; 163ms ago
TriggeredBy: × minecraft@1_17_1.socket
        CPU: 0
And also journalctl -xeu minecraft@1_17_1.service
Nov 04 14:51:01 riley-fedora systemd: minecraft@1_17_1.service: Got no socket.
Nov 04 14:51:01 riley-fedora systemd: minecraft@1_17_1.service: Failed to run 'start' task: Invalid argument
Nov 04 14:51:01 riley-fedora systemd: minecraft@1_17_1.service: Failed with result 'resources'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel 
░░ 
░░ The unit minecraft@1_17_1.service has entered the 'failed' state with result 'resources'.
Nov 04 14:51:01 riley-fedora systemd: Failed to start Minecraft server: 1_17_1.
░░ Subject: A start job for unit minecraft@1_17_1.service has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel 
░░ 
░░ A start job for unit minecraft@1_17_1.service has finished with a failure.
░░ 
░░ The job identifier is 55890 and the job result is failed.
I saw that it was seemingly angry with my minecraft@.socket file, so I ran systemctl status minecraft@1_17_1.socket:
× minecraft@1_17_1.socket - Socket for Minecraft server: 1_17_1
     Loaded: loaded (/usr/local/lib/systemd/system/minecraft@.socket; static)
     Active: failed (Result: resources)
   Triggers: ● minecraft@1_17_1.service
     Listen: /run/minecraft1_17_1.stdin (FIFO)

Nov 04 14:52:35 riley-fedora systemd: minecraft@1_17_1.socket: Failed with result 'resources'.
Nov 04 14:52:35 riley-fedora systemd: Failed to listen on Socket for Minecraft server: 1_17_1.
Nov 04 14:52:41 riley-fedora systemd: minecraft@1_17_1.socket: Failed to open FIFO /run/minecraft1_17_1.stdin: Permission denied
Nov 04 14:52:41 riley-fedora systemd: minecraft@1_17_1.socket: Failed to listen on sockets: Permission denied
Nov 04 14:52:41 riley-fedora systemd: minecraft@1_17_1.socket: Failed with result 'resources'.
Nov 04 14:52:41 riley-fedora systemd: Failed to listen on Socket for Minecraft server: 1_17_1.
Nov 04 14:52:46 riley-fedora systemd: minecraft@1_17_1.socket: Failed to open FIFO /run/minecraft1_17_1.stdin: Permission denied
Nov 04 14:52:46 riley-fedora systemd: minecraft@1_17_1.socket: Failed to listen on sockets: Permission denied
Nov 04 14:52:46 riley-fedora systemd: minecraft@1_17_1.socket: Failed with result 'resources'.
Nov 04 14:52:46 riley-fedora systemd: Failed to listen on Socket for Minecraft server: 1_17_1.
So it seems like the issue has to do with permissions for the pipe I had it use. For good measure, I ran journalctl -xeu minecraft@1_17_1.socket
Nov 04 14:52:46 riley-fedora systemd: minecraft@1_17_1.socket: Failed to open FIFO /run/minecraft1_17_1.stdin: Permission denied
Nov 04 14:52:46 riley-fedora systemd: minecraft@1_17_1.socket: Failed to listen on sockets: Permission denied
Nov 04 14:52:46 riley-fedora systemd: minecraft@1_17_1.socket: Failed with result 'resources'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel 
░░ 
░░ The unit minecraft@1_17_1.socket has entered the 'failed' state with result 'resources'.
Nov 04 14:52:46 riley-fedora systemd: Failed to listen on Socket for Minecraft server: 1_17_1.
░░ Subject: A start job for unit minecraft@1_17_1.socket has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel 
░░ 
░░ A start job for unit minecraft@1_17_1.socket has finished with a failure.
░░ 
░░ The job identifier is 58598 and the job result is failed.
#### Question: What am I doing wrong? I have spent 4-ish hours on the *gasp* second, third, and even fourth pages of Google with no solution. I'm at a loss here, so any and all help is greatly appreciated.
Asked by Riley (65 rep)
Nov 4, 2021, 06:58 PM
Last activity: Dec 21, 2024, 04:24 AM