title: docker: using screen to shut down minecraft server via cron job does not work
body: <p>I want to use docker for a minecraft server. The thing is that I need to shut it down properly to save the world every 24 hours. And to make things more complicated: the people on the server need be informed about this. </p>
  
  <p>So my idea was using "screen" in conjunction with "cron": </p>
  
  <pre class="lang-docker prettyprint-override"><code>################################
  ### We use a java base image ###
  ################################
  FROM openjdk:8 AS build
  
  MAINTAINER me &lt;me@me.com&gt;
  
  #################
  ### Arguments ###
  #################
  ARG PAPERSPIGOT_CI_URL=https://papermc.io/ci/job/Paper-1.13/lastSuccessfulBuild/artifact/paperclip.jar
  
  ##########################
  ### Download paperclip ###
  ##########################
  ADD ${PAPERSPIGOT_CI_URL} /opt/minecraft/server/paperclip.jar
  
  ############################################
  ### Run paperclip and obtain patched jar ###
  ############################################
  RUN cd /opt/minecraft/server/ \
      &amp;&amp; java -jar paperclip.jar; exit 0
  
  RUN cd /opt/minecraft/server/ \
      &amp;&amp; mv cache/patched*.jar paperspigot.jar
  
  ###########################
  ### Running environment ###
  ###########################
  FROM anapsix/alpine-java:latest
  
  ###########################
  ### Install screen      ###
  ###########################
  RUN apk --update add screen
  
  #########################
  ### Working directory ###
  #########################
  WORKDIR /data
  
  ###########################################
  ### Obtain runable jar from build stage ###
  ###########################################
  COPY --from=build /opt/minecraft/server/paperspigot.jar /opt/minecraft/server/paperspigot.jar
  
  ########################
  ### Obtain starth.sh ###
  ########################
  ADD start.sh /opt/minecraft/server/start.sh
  
  ########################
  ### Obtain restart.sh ###
  ########################
  ADD restart.sh /opt/minecraft/server/restart.sh
  
  ###########################################
  ### Configure and run cron              ###
  ###########################################
  COPY crontab /etc/cron/crontab
  # Init cron
  RUN crontab /etc/cron/crontab
  
  CMD ["crond", "-f"]
  
  ###############
  ### Volumes ###
  ###############
  VOLUME "/data"
  
  #############################
  ### Expose minecraft port ###
  #############################
  EXPOSE 25565
  
  ######################################
  ### Entrypoint is the start script ###
  ######################################
  WORKDIR /data
  ENTRYPOINT sh /opt/minecraft/server/start.sh
  </code></pre>
  
  <p>This is the start.sh script that seems to work: </p>
  
  <pre class="lang-shell prettyprint-override"><code>#/bin/sh
  cd /data
  /usr/bin/screen -S minecraft /opt/jdk1.8.0_192/jre/bin/java -jar -Xms800M -Xmx800M -Dcom.mojang.eula.agree=true /opt/minecraft/server/paperspigot.jar
  screen -ls
  </code></pre>
  
  <p>This is the restart script triggered via the crontab. I think it never gets started: </p>
  
  <pre class="lang-shell prettyprint-override"><code>#!/bin/sh
  # me - V1.1 - 18.05.2018
  # Minecraft Server restart
  screen -Rd minecraft -X stuff "say Server is restarting in 30 seconds! $(printf '\r')"
  sleep 23s
  screen -Rd minecraft -X stuff "say Server is restarting in 7 seconds! $(printf '\r')"
  sleep 1s
  screen -Rd minecraft -X stuff "say Server is restarting in 6 seconds! $(printf '\r')"
  sleep 1s
  screen -Rd minecraft -X stuff "say Server is restarting in 5 seconds! $(printf '\r')"
  sleep 1s
  screen -Rd minecraft -X stuff "say Server is restarting in 4 seconds! $(printf '\r')"
  sleep 1s
  screen -Rd minecraft -X stuff "say Server is restarting in 3 seconds! $(printf '\r')"
  sleep 1s
  screen -Rd minecraft -X stuff "say Server is restarting in 2 seconds! $(printf '\r')"
  sleep 1s
  screen -Rd minecraft -X stuff "say Server is restarting in 1 second! $(printf '\r')"
  sleep 1s
  screen -Rd minecraft -X stuff "say Closing server...$(printf '\r')"
  screen -Rd minecraft -X stuff "stop $(printf '\r')"
  sleep 15s
  # echo "Updating to most recent paperclip version."
  # wget -q https://papermc.io/ci/job/Paper-1.13/lastSuccessfulBuild/artifact/paperclip.jar -O /home/pi/minecraft/paperclip.jar
  echo "Restarting now."
  /opt/minecraft/server/start.sh
  </code></pre>
  
  <p>This is the crontab which should start restart.sh every 5 minutes. Does not happen: </p>
  
  <pre class="lang-shell prettyprint-override"><code>*/5 * * * * /opt/minecraft/server/restart.sh
  </code></pre>
  
  <p>Maybe this is the wrong way to do this. Maybe there is a much better way to connect somehow to the screen session or to restart the server with a message somehow. But I did not find anything online. Any idea?</p>