Can't start Minecraft server in tmux with systemctl

0

I'm trying to automate my Minecraft server because having to manually wrangle tmux sessions gets really old after a while. Here's the systemd service file I wrote to help me with that:

[Unit]
Description=Minecraft Server: %i
After=network.target

[Service]
WorkingDirectory=/opt/minecraft/servers/%i
User=minecraft
Group=minecraft

Restart=on-failure

ExecStart=/usr/bin/tmux new -s mc-%i -d '/usr/bin/java -Xms1G -Xmx6G -jar forge-universal.jar nogui' bash

ExecStop=/usr/bin/tmux send -t mc-%i 'say SERVER WILL SHUT DOWN IN 10 SECONDS' ENTER
ExecStop=/bin/sleep 10
ExecStop=/usr/bin/tmux send -t mc-%i 'stop' ENTER

[Install]
WantedBy=multi-user.target

Executing the ExecStart line manually works perfectly, the Java process is neatly put into a detached tmux session that I can attach to at my leisure.

Trying start the server via systemctl start minecraft@creative fails miserably, with dozens of error messages like these filling up the journal before finally failing:

Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Service hold-off time over, scheduling restart.
Oct 11 21:34:45 kingcolour systemd[1]: Stopped Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: Starting Minecraft Server: private...
Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Control process exited, code=exited status=1
Oct 11 21:34:45 kingcolour systemd[1]: Failed to start Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Unit entered failed state.
Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Failed with result 'exit-code'.
Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Service hold-off time over, scheduling restart.
Oct 11 21:34:45 kingcolour systemd[1]: Stopped Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: Starting Minecraft Server: private...
Oct 11 21:34:45 kingcolour systemd[1]: Started Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Control process exited, code=exited status=1
Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Unit entered failed state.
Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Failed with result 'exit-code'.
Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Service hold-off time over, scheduling restart.
Oct 11 21:34:45 kingcolour systemd[1]: Stopped Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Start request repeated too quickly.
Oct 11 21:34:45 kingcolour systemd[1]: Failed to start Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Unit entered failed state.
Oct 11 21:34:45 kingcolour systemd[1]: minecraft@private.service: Failed with result 'exit-code'.

Since tmux swallows all the error messages that could've happened, I have no idea what could be going wrong. Has anyone encountered this before?

Peter W.

Posted 2018-10-11T19:09:55.103

Reputation: 706

please add the output of journalctl -u minecraft@creative.service -b to your question. please edit your question and do not respond in the comments.

– Nordlys Jeger – 2018-10-11T19:36:02.543

Have added the last 20 lines of journalctl spam instead of just the last 7. – Peter W. – 2018-10-11T19:38:43.467

What is the point of the bash argument at the end? It looks like you're giving 2 different commands to tmux to run? – mjb2kmn – 2018-10-11T19:50:31.093

Goddammit. God flipping dammit. I was experimenting with different versions of the service file since none of them seemed to work, but I must've missed that part. Removing that errant bash makes the script work. – Peter W. – 2018-10-11T19:54:08.860

Answers

2

systemd expects a service to stay running in the foreground in order to monitor it. When you run tmux detached, with -d, it forks a new process and returns the original command. I've not confirmed this with tmux and minecraft but try setting Type=forking in the Service section to tell systemd to track the child process forked from the command specified in ExecStart.

Side note: Are you sure you need tmux here? It's much simpler without.

mjb2kmn

Posted 2018-10-11T19:09:55.103

Reputation: 526

Adding Type=forking didn't do anything, sadly. I'm getting the exact same error messages. And I do need tmux here. Starting Minecraft results in an interactive prompt that I need to administer the server. – Peter W. – 2018-10-11T19:36:00.120

Scratch that. This, combined with mjb2kmn's comment, made the script work. Thanks! – Peter W. – 2018-10-11T19:54:46.920