Cannot stop screen started with systemctl start [closed]

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?


Solution 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