Garry's Mod

Garry's Mod

View Stats:
NooK Apr 4, 2024 @ 3:49pm
Finally a decent batch file solution to a Garrys Mod Server Auto-Restart and Crash Detection Watchdog.
Good morning folks.
Thought I'd share my current working solution to auto-restarting my Garry's mod server when it stalls, crashes, or just needs an automatic restart to the server to a set time.

IT SHOULD BE NOTED
That you need to edit the files a little bit before you get started.

First off the files needs to be placed in your server root folder that contains srcds.exe

This guide will contain 3 Batch Files that should be edited and placed in the root folder.

I will format each section the same way like so:

Title of Batch File
Short Description of Batch File
The Batch Code Box

Thing 1 that needs to be changed
Variable

Then a detailed description of what the file does exactly.

So we will start with the Core Batch File I call it the start me file.

StartMe.bat
This file is the anti-crash and start up batch file, it starts the server and auto-restart batch files respectively.
@ECHO off SET _PollingInterval=30 TITLE "DLRRP Watchdog Program" start autorestart_watchdog.bat :BatchStart start dlrrp_server.bat :Start TIMEOUT /T %_PollingInterval% SET PID= FOR /F "tokens=2 delims= " %%i IN ('TASKLIST ^| FIND /i "srcds.exe"') DO SET PID=%%i IF [%PID%]==[] ( ECHO //================================================\\ >>watchdog.log ECHO ║ Application was not running. Restarting script. >>watchdog.log Echo ║ TIME: %TIME% DATE: %DATE% >>watchdog.log ECHO \\================================================// >>watchdog.log GOTO BatchStart ) GOTO Start GOTO:EOF

_PollingInterval
Number of seconds between every anti-crash check. The lower the quicker it checks to restart the server.
server_commandline.bat
This needs to be changed if you decide to rename the server_commandline.bat file.

This initial batch file:
  • Starts the server.
  • Checks every (_PollingInterval) seconds to see if the srcds.exe program is running in task manager. If not, it will print a message in the cmd prompt.
  • Logs crashes and restarts to the watchdog log file automatically.
  • Creates a watchdog log file automatically.

server_commandline.bat
The bat file that contains your usual server setup .bat file and command arguements.
start srcds.exe -console -nohltv -condebug -tickrate 22 +gamemode sandbox +maxplayers 10 +map "gm_flatgrass" +fps_max 66 +host_workshop_collection 1234 +sv_setsteamaccount 123 exit

gamemode
Name of your gamemode
tickrate
The rate your server updates per second the higher the more responsive, the lower the better performance of the server.
maxplayers
The player count max on the server.
map
The exact name of the map as shown in the `map showall` console command.
fps_max
The Frames Per Second the server will run maximum.
host_workshop_collection
The ID of the workshop collection.
sv_setsteamaccount
Steam Account Auth Key, this will effect your visibility in the server list.

This is the usual command line batch file for the server, this is the actual window that will appear as the server console you can input commands with.

Lastly we have the auto-restart watchdog.
autorestart_watchdog.bat
This file polls the system time and when it meets the set time in the file it kills the server.
@echo off echo %date% %time% - DLRRP Watchdog started >>watchdog.log :: Gmod Server Watchdog by NooK! :D :: Set time for shutdown set endertime=15:17 :: DO NOT TOUCH ANYTHING BELOW THIS LINE FOR GODS SAKE. set shutdwntime=%endertime%:00.01 set gaptimevar=%endertime%:01.00 :loop if %TIME% GEQ %shutdwntime% ( if %TIME% LSS %gaptimevar% ( goto kill ) ) :: if %TIME% GEQ 15:01:00.00 and LSS 15:02:00.00 goto kill tasklist | FIND /i "srcds.exe">NUL cls echo The server will shutdown at %shutdwntime% it is currently, %time% the server should still be running. set /a loopedtime=loopedtime+1 timeout 1 >nul goto loop :kill echo %date% %time% - %totaltime% Server auto-restart initiated, killing server. >>watchdog.log taskkill /f /im srcds.exe goto graceful :graceful echo %date% %time% - Server shut down gracefully at %time%. >>watchdog.log goto loop

endertime
In 24H Format the HourHour:MinutesMinutes of when you want the server to reset.

This file will grab the SYSTEM TIME (IMPORTANT TO NOTE IT IS SYSTEM TIME, NOT YOUR LOCAL PC TIME UNLESS INSTALLED LOCALLY.) and compare it to the config you set in the bat file. When the times meet up for 1 second, (About 6-10 ticks) it will send kill messages to the system to destroy the srcds program. IT DOES NOT RESTART THE PROGRAM. The startme.bat will do that for you!

I hope this helps you guys.


P.S.
This will kill every instance of SRCDS.exe that you have on your system controlled by your account. It does not account for multiple programs.

So if your running two servers with these files, it will not detect a crash if only one server goes down.
Last edited by NooK; Apr 4, 2024 @ 3:52pm
< >
Showing 1-2 of 2 comments
yETI Apr 4, 2024 @ 6:39pm 
ty
NooK Apr 4, 2024 @ 6:54pm 
Originally posted by IceBound:
ty
No problem!
< >
Showing 1-2 of 2 comments
Per page: 1530 50

Date Posted: Apr 4, 2024 @ 3:49pm
Posts: 2