Cron Job to restart servers

Discussion in 'Systems Administration' started by Keystirras, Jun 18, 2016.

  1. Hello, I'm looking for a CRON job to restart my servers. Anybody have one? I'm looking for it to restart about 2 AM every day.
     
  2. What Linux distro are you using?
     
  3. do you want to restart the hole server or only the mc servers ?
     
    • Like Like x 1
    • Agree Agree x 1
  4. do you use screens ?
     
    • Informative Informative x 1
  5. do /stop command in server
    make a restart.sh
    screen -X -S (session # you want to kill) quit
    cronjob it ( IDK what times you want it done )
    then chmod 777 restart.sh
    Boom. Done. Contact me if you need help
     
    • Agree Agree x 1
  6. How do I make it so linux will send the /stop command? to the server?
     
  7. I dont think you can.. Unless theres a remote stop plugin or something, but you cant do it in ssh unless you end the session
     
  8. This is a script I made to restart my servers:
    Code (Text):
    #!/bin/sh
    echo "Letting people know that the server is going to restart...."
    screen -R screenname -X stuff "say Server is restarting in 5 minutes to avoid lag! $(printf '\r')"
    sleep 240s
    screen -R screenname -X stuff "say Server is restarting in 1 minute! $(printf '\r')"
    sleep 30s
    screen -R screenname -X stuff "say Server is restarting in 30 seconds! $(printf '\r')"
    sleep 20s
    screen -R screenname -X stuff "say Server is restarting in 10 seconds! $(printf '\r')"
    sleep 1s
    screen -R screenname -X stuff "say Server is restarting in 9 seconds! $(printf '\r')"
    sleep 1s
    screen -R screenname -X stuff "say Server is restarting in 8 seconds! $(printf '\r')"
    sleep 1s
    screen -R screenname -X stuff "say Server is restarting in 7 seconds! $(printf '\r')"
    sleep 1s
    screen -R screenname -X stuff "say Server is restarting in 6 seconds! $(printf '\r')"
    sleep 1s
    screen -R screenname -X stuff "say Server is restarting in 5 seconds! $(printf '\r')"
    sleep 1s
    screen -R screenname -X stuff "say Server is restarting in 4 seconds! $(printf '\r')"
    sleep 1s
    screen -R screenname -X stuff "say Server is restarting in 3 seconds! $(printf '\r')"
    sleep 1s
    screen -R screenname -X stuff "say Server is restarting in 2 seconds! $(printf '\r')"
    sleep 1s
    screen -R screenname -X stuff "say Server is restarting in 1 second! $(printf '\r')"
    sleep 1s
    screen -R screenname -X stuff "say Closing server...$(printf '\r')"
    screen -R screenname -X stuff "stop $(printf '\r')"
    sleep 15s
    screen -R screenname -X stuff "bash start.sh $(printf '\r')"
    echo "Done"
    You will have to use crontab if you want to automatically execute it at the given periods. If you want to execute it every day at 2AM, you could have something like this:
    Code (Text):
    0 2 * * * /scripts/svreload.sh >> /logs/bscript.log 2>&1

    or just

    0 2 * * * /scripts/svreload.sh >> /dev/null 2>&1

    If you don't want to keep a reload script log

    Another option:

    Use this as your start script:
    Code (Text):
    cd "$( dirname "$0" )"
    while true
    do
      java -Xmx1G -Xms1G -jar spigot.jar
      echo "If you want to completely stop the server process now, press Ctrl+C before
      the time is up!"
    echo "Rebooting in:"
    for i in {10..1}
    do
         echo "$i..."
         sleep 1
    done
    echo "Rebooting now!"
    done
    Use this as your cron job:
    Code (Text):
    0 2 * * * screen -R screenname -X stuff "stop $(printf '\r')" >> /logs/bscript.log 2>&1

    or just

    0 2 * * * screen -R screenname -X stuff "stop $(printf '\r')" >> /logs/bscript.log >> /dev/null 2>&1

    If you don't want to keep a reload script log

    If you still have questions, feel free to send me a PM, I am willing to help anyone for free :).
     
    #14 DeltaSV, Jun 28, 2016
    Last edited: Jun 28, 2016