Minecraft server automatic restarter

Please be aware that this is not a fool-proof script! If Minecraft crashes in a way that it keeps running (the process freezes), this script will not work. This is a common occurrence if you are running mods. Also if your world somehow gets corrupted by fatal bugs or memory errors, the script will just keep rebooting endlessly.

This script is divided in two parts. The main START script which keeps the process alive in a loop and a STOP script will stop the loop. The stop script does this by writing a file, telling the main loop to stop. If you accidentally loose or forget the STOP script the only way to stop the loop is by terminating “wscript.exe” from the taskmanager.

You can either copy the code from below and paste it into notepad or your favourite text editor or download the scripts directly.

Please note that the reference to Server Start.bat is a simple batch file containing the following code:

java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui

START script

Direct download of MinecraftServerLoop-Start.vbs Depending on your browser settings you may have to right click and select save link/target as.
(GitHub)

'===================================================================================
'SETTINGS
'===================================================================================

'Change this to your server launcher
serverFile = "Server Start.bat"

'Change this file name to whichever you prever
logFileName = "MCServerCrashLog.txt"

'Internal wait timer to prevent accidentally overflowing the log file
waitTimer = 2000

'Show more junk while running, only for testing purposes
debugMode = false

'===================================================================================
'DECLARATIONS
'===================================================================================

'Declare Shell object in order to start other programs
set shellServer = WScript.CreateObject("WScript.Shell")

'Declare variables
Dim localPath, crashCount
'Find local path where the script run from
localPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
'Set the application path
appPath = localpath & "\" & serverFile

'Declare Log function variables
Dim logFile, logFSO
logFile = localPath & "\" & logFileName

'Declare File System object in order to write to log file
Set logFSO = CreateObject("Scripting.FileSystemObject")
Set stopCommandFSO = CreateObject("Scripting.FileSystemObject")

If logFSO.FileExists(logFile) Then
	'Delete old log when starting if there is one
	logFSO.DeleteFile logFile
End If 

'===================================================================================
'PROGRAM
'===================================================================================

Log "The server started for the first time."

'Master loop
Do
	'Abort the loop if command is saved
	If stopCommandFSO.FileExists(localPath & "\StopLoop.nothing") Then
		Log "The server automatic reboot loop has been stopped."
		stopCommandFSO.DeleteFile localPath & "\StopLoop.nothing"
		wscript.quit
	End If 
	
	
	'Start the server and wait for it to crash or terminate
	Return = shellServer.Run("CMD /C CD "& localpath & " & """ & AppPath & """" , 1, true)
	
	'Count number of crashes/restarts this far
	crashCount = crashCount + 1
	
		'Write to log file
	Log "The server has crashed or restarted. Total " & crashCount & " times." 
	
	'More information
	if debugMode = true then
		WScript.Echo "The server has crashed or restarted. Total " & crashCount & " times since starting." 
	end if

	'Internal wait timer to prevent accidentally overflowing the log file
	WScript.Sleep waitTimer
Loop
wscript.quit

'===================================================================================
'FUNCTIONS
'===================================================================================

Function checkProcess(processName)
	'Get list of running processes
	Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process")
	'Go through every item in the process list
	For Each objProcess in colProcessList
		'Return value if process was found
		If objProcess.name = processName then
			checkProcess = True 
		else
			checkProcess = False
		End if
	Next
end Function

Function killProcess(processName)
	'Get list of running processes
	Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process")
	'Go through every item in the process list
	For Each objProcess in colProcessList
		'Return value if process was found
		If objProcess.name = processName then
			'Terminate process
			objProcess.terminate
		End if
	Next
end Function

Sub Log (msg)
    Set stream = logFso.OpenTextFile(logFile, 8, True)
    stream.writeline date & " " & time & ": " & msg
    stream.close
end sub

STOP script

Direct download of MinecraftServerLoop-Stop.vbs Depending on your browser settings you may have to right click and select save link/target as.
(GitHub)

'===================================================================================
'DECLARATIONS
'===================================================================================

'Declare variables
Dim localPath, crashCount
'Find local path where the script run from
localPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

'Declare File System object in order to write to files
Set stopCommandFSO = CreateObject("Scripting.FileSystemObject")
stopCommandFile = localPath & "\StopLoop.nothing"

'===================================================================================
'PROGRAM
'===================================================================================

Write "This file will stop the loop."
wscript.quit

'===================================================================================
'FUNCTIONS
'===================================================================================

Sub Write (msg)
    Set stream = stopCommandFSO.OpenTextFile(stopCommandFile, 8, True)
    stream.writeline date & " " & time & ": " & msg
    stream.close
end sub
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s