Skip to content

Instantly share code, notes, and snippets.

Forked from nathanielc/gist:9b98350ccbcbf21256d7
Created November 13, 2015 01:38
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save winny-/bb17853ffc76fbb9b039 to your computer and use it in GitHub Desktop.
Save winny-/bb17853ffc76fbb9b039 to your computer and use it in GitHub Desktop.
Minecraft systemd unit file that uses tmux instead of screen
[root@plex ~]# cat /etc/systemd/system/minecraft@.service
Description=Minecraft Server %i
ExecStart=/usr/bin/tmux new-session -s mc-%i -d '/bin/java -Xmx2048M -jar minecraft_server.jar nogui'
ExecStop=/usr/bin/tmux send-keys -t mc-%i:0.0 'say SERVER SHUTTING DOWN. Saving map...' C-m 'save-all' C-m 'stop' C-m
ExecStop=/bin/sleep 2
# HowTo
# Create directory in /opt/minecraft-XX where XX is a name like 'survival'
# Add minecraft_server.jar into dir with other conf files for minecraft server
# Enable/Start systemd service
# systemctl enable minecraft@survival
# systemctl start minecraft@survival
# To run multiple servers simply create a new dir structure and enable/start it
# systemctl enable minecraft@creative
# systemctl start minecraft@creative
Copy link

I've created a very similar unit file, but mine will only allow one instance to run at a time. Upon starting a nth instance it is immediately stopped by systemd. As you can see I tried to do a pidfile, but that doesn't seem to be helping. Have you had any issues?

Description=Spigot Minecraft Server %i


ExecStart=/usr/bin/tmux new-s -s mcs-%i -d '/usr/bin/java -Xmx${jvm_mem} -jar spigot.jar nogui'

#ExecStartPost=/home/miner/mcs/server-tools/scripts/mkpid %i
ExecStartPost=/usr/bin/tmux splitw -p 50 -t mcs-%i:0

ExecStop=/usr/bin/tmux send-keys -t mcs-%i:0.0 'say SERVER SHUTTING DOWN in 5s' C-m
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/tmux send-keys -t mcs-%i:0.0 'save-all' C-m 'stop' C-m
ExecStop=/bin/sleep 2
ExecStop=/usr/bin/tmux kill-session -t mcs-%i


/usr/bin/tmux list-panes -F '#{pane_active}:#{pane_pid}' -t mcs-$1 | /bin/grep '^0' | /usr/bin/cut -f2 -d: > /home/miner/mcs/$1/assets/

chmod o=r /home/miner/mcs/$1/assets/

Copy link

winny- commented Mar 6, 2018

Holycrap, i was cleaning out old gists and saw you commented on this less than a fortnight ago. No need to use a PID file, systemd should track that for you, I think? Hope that helps.

Copy link

d5c4b3 commented Apr 3, 2018

For anyone getting 'No server found on' errors from tmux:
If the 'mcserver' user's default shell is /bin/false (like if you created a system/service account) then change
ExecStart=/usr/bin/tmux new-session -s mc-%i -d '/bin/java -Xmx2048M -jar minecraft_server.jar nogui'
ExecStart=/usr/bin/tmux new-session -s mc-%i -d '/bin/java -Xmx2048M -jar minecraft_server.jar nogui' bash

Tmux does not work with system users because it opens a login shell which is disabled for them. Specifying bash (or sh or whatever) will cause tmux to start a non-login shell.

Copy link

This is good stuff. Thanks!

Copy link

Good stuff and ty guys.

Would either of you be interested in helping with my next project?

With mcmyadmin going the way of AMP, and in my exp is a pain for headless linux anyway. I am looking to set my startup script to not just launch a console available like this as a service, but to also monitor users online, and set it to shut down when not in use, and automatically wake when a user tries to connect. (Sending of a message comes later).

So far I am able to launch a valid tmux and set to available use of the console as a service. I want to pair it or fork it with another fuction though in order to achieve this.

I first tested using an Rcon command line monitor at intervals, issuing the “list” command to console for players, but feel that’s too heavy and not needed.

Currently I haven’t fully researched yet, but my ideas are leaning toward a few options and maybe some direction might help.

My first thought was to attempt to pipe an interval of the results of a socket monitor but there are many flaws in that design.

Otherwise I was looking into possibly using something along the lines of netstat , bam-no, or another means to either monitor the actual packet flow, or monitor the packets themselves also.

My theory was to create a monitor that when 0 players online, would shut down the server, and also, if a player tried to connect, it would spoil the server up automatically. (With a wait time ofcourse) , as for the message to my players I was looking more toward a packet monitor/injection means to catch the exact packet of an attempted connection from a minecraft client, and reconfigureing the packets sent for “server error offline” to “ server booting now please try again in 5 mins” .

Yes I know there are solutions out there, but one I crave knowledge , and two I hate paying......I want to create an open source solution to this for other admins, and would like some help. I am not the best versed, but put me on a path and I’ll see it through, looking for like minded individuals willing to put in the work with me.

Let me know if you might be interested

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment