I use screen to run a Minecraft server, and I restart it every day. This is my start code:

screen -mdS mcserver
screen -S mcserver -p 0 -X stuff "java -Xmx4G -jar server.jar^M"

This is my stop code:

screen -S mcserver -p 0 -X stuff "stop^M"
sleep 10
screen -XS mcserver quit

Sending the command stop makes the Minecraft server save the world, which can take a few seconds. Is there a way to make the program wait until java (The Minecraft server) isn't running and then kill screen? If I kill screen before I send stop to Minecraft, the world can have a few glitches. (Player items desync from the world blocks, people lose items, and accidentally duplicate stuff)

  • I find it much simpler and safer to let systemd do all that work. My server runs as a systemd service (instead of multiple screen scripts), and restart/backup is on a systemd timer. Server output is logged anyway. – user535733 Jan 7 at 15:28

seems scriptable to me. You could try doing something simply like this


#Stop the minecraft server
screen -S mcserver -p 0 -X stuff "stop^M"

while true
  echo "checking for java..."
  sleep 2
  ps aux |grep 'server.jar' |grep -v grep
  [ $? -eq 1 ] && break

screen -XS mcserver quit

This basically loops until server.jar is not found in the process list, then it breaks out of the loop. Since the loop is broken, it should be safe to shutdown the screen session

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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