0

On ubuntu server 20.04

I created a service definition for a Minecraft server within a screen session like that

[Unit]
Description=Minecraft Server
After=network.target
 
[Service]
User=minecraft
Nice=1
KillMode=none
SuccessExitStatus=0 1
#ProtectHome=true
ProtectSystem=full
PrivateDevices=true
NoNewPrivileges=true
WorkingDirectory=/home/minecraft
ExecStart=/usr/bin/screen -AmDS "minecraft-server" /usr/bin/java -Xmx12G -Xms4G -jar /home/minecraft/minecraft-server.jar nogui
ExecStop=/usr/bin/screen -r "minecraft-server" -X quit
ExecStopPost=kill -9 $(ps -ef | grep '[/]home/minecraft/minecraft-server.jar' | grep -v SCREEN | awk '{print $2}')
ExecStopPost=rm -f /home/minecraft/Blumentopf/session.lock
Restart=always
RestartSec=2

[Install]
WantedBy=multi-user.target

What works

  • systemctl start minecraft.service ⇒ screen session start and user Minecraft can enter it
  • log in as user minecraft and execute screen -AmDS "minecraft-server" /usr/bin/java -Xmx12G -Xms4G -jar
    • it blocks the terminal
    • from another shell I can enter the screen session
  • log in as user minecraft and execute screen -r "minecraft-server" -X quit quits the running Minecraft server
  • killing running java application of running service immediately restarts the service

What does not work

  • systemctl stop minecraft.service ⇒ this simply hangs for several seconds and when it returns, the service is still running. Yes still running, not restarted (same PID, no disconnect, no new log)

I can then login into the screen session and stop the service without it being auto restarted by systemctl. But what the heck? The commands work as expected, why cannot systemctl stop (or restart) the java in process in the screen session?

1
  • I think this may relate to the "KillMode" setting. I added an answer with a link to the man page for this setting. – Forever Cuber Apr 9 at 18:29
1

Try changing the service definition line KillMode=none to KillMode=control-group.

Please test that if you could; Unless you have some purpose for running in mode none.

man: systemd.kill

1
  • 1
    thanks for the documentation link. It was the missing piece. I even could comment out the ExecStop line, as screen propagates SIGTERM to the internal process, which is enough to cleanly shutdown the server! – Jan Apr 9 at 21:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.