Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Automatic server restart with chat warning

Discussion in 'Modding' started by TopHatTim, Oct 12, 2017.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. TopHatTim Trainee Engineer

    Messages:
    12
    I wrote this script a while ago for my server to help with automating server restarts, figured I might as well share it :)


    Features
    • Checks server status every 60 seconds
    • Automatically starts the server if down
    • Restarts the server after a set amount of time (default of 480 mins/8 hours)
    • Optionally usage of server messages mod for sending warning messages about server restart

    Settings
    • service - The name of your server service in quote marks
    • serviceString - Again the name of your server service but without quote marks (I'll try and make this better next time I get around to it ... )
    • timedRestart - Boolean, enables the automatic restart after x amount of time
    • checkCount - Integer, sets the variable for current number of checks. Leave as 0
    • timer - Integer, sets the number of times to check the server before restarting
    • timeToRestart - Integer, variable that keeps track of time left to restart
    • serverMessages - Boolean, set to true to enable server messages mod integration
      If you want to make use of the server messages mod you must change the four directory lines to point to your medieval engineers directory.
      ("C:\ProgramData\MedievalEngineersDedicated\%serviceString%\Storage\900367518.sbm\SendMessageHere.txt")

    Instructions

    For windows servers only - tested on Windows server 2012
    Save this code as a .bat file, change the settings to be suitable for your server and then run it


    Code:
    @echo off
    set service="Top Hat Engineers"
    set serviceString=Top Hat Engineers
    set timedRestart=true
    set checkCount=0
    set timer=480
    set timeToRestart=480
    set serverMessages=false
    
    :start
    sc start %service%
    echo %ERRORLEVEL%
    
    if %ERRORLEVEL% == 0 (
    	echo Server starting ...
    	goto loop
    )
    if %ERRORLEVEL% == 1056 (
    	goto loop
    )
    if %ERRORLEVEL% == 1 (
    	goto error
    )
    
    :error
    echo There was a problem!
    pause
    exit
    
    :loop
    timeout /t 60 /nobreak
    cls
    sc query %service% | find "RUNNING"
    
    if %ERRORLEVEL% == 0 (
    	rem cls
    	echo Query value: %ERRORLEVEL%
    	echo %checkCount%: Running
    	set /a checkCount=%checkCount%+1
    ) else (
    	echo %checkCount%: Not running
    	set checkCount=0
    	goto start
    )
    
    if %timedRestart% == true (
    	if %serverMessages% == true (
    		set /a timeToRestart=%timer%-%checkCount%
    		if 30 == %timeToRestart% (
    			echo Server will restart in %timeToRestart% minutes...
    			echo Server will restart in %timeToRestart% minutes...>>"C:\ProgramData\MedievalEngineersDedicated\%serviceString%\Storage\900367518.sbm\SendMessageHere.txt"
    		)
    		if 15 == %timeToRestart% (
    			echo Server will restart in %timeToRestart% minutes...
    			echo Server will restart in %timeToRestart% minutes...>>"C:\ProgramData\MedievalEngineersDedicated\%serviceString%\Storage\900367518.sbm\SendMessageHere.txt"
    		)
    		if 10 == %timeToRestart% (
    			echo Server will restart in %timeToRestart% minutes...
    			echo Server will restart in %timeToRestart% minutes...>>"C:\ProgramData\MedievalEngineersDedicated\%serviceString%\Storage\900367518.sbm\SendMessageHere.txt"
    		)
    		if 5 GEQ %timeToRestart% (
    			echo Server will restart in %timeToRestart% minutes...
    			echo Server will restart in %timeToRestart% minutes...>>"C:\ProgramData\MedievalEngineersDedicated\%serviceString%\Storage\900367518.sbm\SendMessageHere.txt"
    		)
    	)
    	if %checkCount% GTR %timer% (
    		sc stop %service%
    		echo %ERRORLEVEL%
    		
    		set checkCount=0
    		set timeToRestart=%timer%
    		
    		timeout /t 60 /nobreak
    		goto start
    	)
    )
    
    goto loop
    pause
    

    I want to come back and make it a little nicer to use, adding things like command line arguments instead of having to set variables and settings the directory to use a variable
     
Thread Status:
This last post in this thread was made more than 31 days old.