Jump to content

Linux Automatic Server Restart Script - looping script /or/ crontab + bash


izakt

Recommended Posts

Linux Automatic Server Restart Guide

Date: August 20th 2022

Guide Written During v0.7.0.126

Created on Linux / Centos 7

(with nano installed)

edited Oct 2022

 

Hi I thought I would dump this here. Maybe it can help someone.:2_grimacing: I am a noob. plz critic :3_grin: All this was done in putty.exe

 

Summary: Below I try to explain how you can create a script that will 1) check if your game server goes offline and 2) reboot your game server automatically if it detects the server is offline.

 

  • 1. Use a looping script
  • 2. Use crontab to check if the server is offline, and launch the game if it is offline. (edited)
  • 3. Use crontab to automatically shut down the game server properly, and then relaunch the game server. (new)

 

1. Use a looping script
I wanted to create a script that would keep my game server online 24/7 without manually checking it. 

I have my game hosted in a Linux server with centos. It was pretty straight forward, from someone that has never done something like this.

 

You can use the code below to create a bash script in the root folder.
 

create a bash script:

# nano autoRestartServer.sh

use this to create the script:

#! /usr/bin/bash

while :
do
#Check server status
curl --fail -LI <IP:PORT> -o /dev/null -w '%{http_code}\n' -s > /dev/null
#if offline
if [ $? -eq 7 ]; then
    echo "Server Offline..."

    #Check for existing screen
    if ! screen -list | grep -q "myGameServer"; then
    screen -d -m -S myGameServer
    echo "created new screen..."
    fi

    #Launch game server
    screen -S myGameServer -X stuff 'cd Path/To/Server/\r'
    screen -S myGameServer -X stuff 'LD_LIBRARY_PATH=. mono "Intersect Server.exe"\r'
    echo "Game server started..."
    echo "sleep 2m..."
    sleep 2m
else
    echo "Server Online..."
    echo "sleep 2m..."
    sleep 2m
fi

done

Above you need to edit three things.

1) Line 6 - you need to replace "<IP:PORT>" with the game server's ip and port number. Ex: "12.123.123.12:9999"

2) Line 18 - you need to replace "Path/To/Server/" to the absolute path where your game server is located. Ex: "MyGame/LiveServer/version1/"

*NOTE: make sure you don't remove the "\r" at the end of the line.

3) Line 19 - you may need to replace "Intersect Server.exe" with the actual name of your game's server. Ex: "My Game Server.exe"

Then save the file with crtl+O -> [Enter]

And exit the file ctrl+X

 

Now that the script is created, then you would simply open a new screen

# screen

and run the script

# bash autoRestartServer.sh

then detach from the screen (ctrl+a -> d) and it should keep running / checking the server forever.

If you need to take your server offline you can reattach to the screen running the looping script, and just kill the screen to stop the script. ctrl+a -> k -> y

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

2. Use crontab to check if the server is offline, and launch the game if it is offline. (edited)
A looping script probably isn't a good idea. Crontab is a better idea. It will be able to run a script intervals of time automatically.

So we will create a bash script that is ran by crontab every 2 ~ 3 minutes.

 

create a bash script:

# nano cronCheckServer.sh

use this to create the script:

#! /usr/bin/bash

#Check server status
curl --fail -LI <IP:PORT> -o /dev/null -w '%{http_code}\n' -s > /dev/null
#If offline
if [ $? -eq 7 ]; then
    echo "Server Offline..."
    echo "kill screen..."
    screen -X -S myGameServer quit
    echo "sleep 3s..."
    sleep 3s
    echo "create new screen..."
    screen -d -m -S myGameServer
    echo "sleep 3s..."
    sleep 3s
    #Launch game server
    screen -S myGameServer -X stuff 'cd Path/To/Server/\r'
    screen -S myGameServer -X stuff 'LD_LIBRARY_PATH=. mono "Intersect Server.exe"\r'
    echo "Game server started..."
else
    echo "Server Online..."
fi

Above you need to edit three things.

1) Line 4 - you need to replace "<IP:PORT>" with the game server's ip and port number. Ex: "12.123.123.12:9999"

2) Line 17 - you need to replace "Path/To/Server/" to the absolute path where your game server is located. Ex: "MyGame/LiveServer/version1/"

*NOTE: make sure you don't remove the "\r" at the end of the line.

3) Line 18 - you may need to replace "Intersect Server.exe" with the actual name of your game's server. Ex: "My Game Server.exe"

Then save the file with crtl+O -> [Enter]

And exit the file ctrl+X

 

Now that the script is created, you then need to set up cron to run the script

# EDITOR=nano crontab -e

and you need to create a task to run the script every 1 ~ 3 minutes

*/3 * * * * /bin/bash/ /root/cronCheckServer.sh

Then save the file with crtl+O -> [Enter]

And exit the file ctrl+X

 

if all is well, crontab should run in about 3 minutes and the server should launch.

You shouldn't have to do anything from here. If the game crashes, it should be back online within 3 minutes.

 

However, if you want to take the game offline, you'll need to disable the crontab task.

Its pretty easy, you can just disable the task in crontab.

# EDITOR=nano crontab -e

And you can just add a hashtag "#" in front of the task to disable it:

# */3 * * * * /bin/bash/ /root/cronCheckServer.sh

Then save the file with crtl+O -> [Enter]

And exit the file ctrl+X

Then the server should not start back up again until you remove the hashtag (#) from the crontab task.

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

3. Use crontab to automatically shut down the game server properly, and then relaunch the game server.
This would be a bash script that is ran by crontab at your choice. Its going to restart the server, so it depends on your situation. I it run every sunday at midnight. You could, for example, set it to run every 24 hours.

 

create a bash script:

# nano cronRestartServer.sh

use this to create the script:

 

#! /usr/bin/bash

#Check server status
curl --fail -LI <IP:PORT> -o /dev/null -w '%{http_code}\n' -s > /dev/null
#If offline
if [ $? -eq 7 ]; then
    echo "Server Offline..."
else
    echo "Server Online... starting server restart..."
    echo "Announcement: Server will shut down in 10 minutes!..."
    screen -S myGameServer -X stuff 'announcement "The server will shut down in 10 minutes!"\r'
    echo "sleep 5m..."
    sleep 5m
    echo "Announcement: Server will shut down in 5 minutes!..."
    screen -S myGameServer -X stuff 'announcement "The server will shut down in 5 minutes!"\r'
    echo "sleep 3m..."
    sleep 3m
    echo "Announcement: Server will shut down in 2 minutes!..."
    screen -S myGameServer -X stuff 'announcement "The server will shut down in 2 minutes!"\r'
    echo "sleep 1m..."
    sleep 1m
    echo "Announcement: Server will shut down in 1 minute!..."
    screen -S myGameServer -X stuff 'announcement "The server will shut down in 1 minute!"\r'
    echo "sleep 1m..."
    sleep 1m
    screen -S myGameServer -X stuff 'announcement "The server will shut down now!"'
    screen -S myGameServer -X stuff '^C\r'
    echo "server shut down..."
    echo "kill screen..."
    screen -X -S myGameServer quit
    echo "sleep 3s..."
    sleep 3s
    echo "create new screen..."
    screen -d -m -S myGameServer
    echo "sleep 3s..."
    sleep 3s
    #Launch game server
    screen -S myGameServer -X stuff 'cd Path/To/Server/\r'
    screen -S myGameServer -X stuff 'LD_LIBRARY_PATH=. mono "Intersect Server.exe"\r'
    echo "Game server started..."
fi

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...