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]: [email protected]: 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]: [email protected]: 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]: [email protected]: Unit entered failed state.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Failed with result 'exit-code'.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: 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]: [email protected]: Control process exited, code=exited status=1
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Unit entered failed state.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Failed with result 'exit-code'.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: 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]: [email protected]: 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]: [email protected]: Unit entered failed state.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: 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?
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments