What do you use on dedicated server to auto-restart a crashed server?

Tehnomaag

New member
I am running a small private dedicated 7d2d server for myself and few friends. However, the server crashes from time to time, not frequently but at least once a week or so. This is a bit annoying as it often happens when I have already gone to sleep and my friends have to wait then with their things until I wake up and are able to restart the server. So the question is - what do you use to chek if the server is running and re-start it if it has crashed?

The server is running on a dedicated physical machine with Windows 8.1 operating system. 

Basically what I am looking for is something that cheks if there exists a task in task manager called "7DaysToDieServer" and if it does not exist then run a "D:\7 Days to Die Dedicated Server\startdedicated.bat" script. This would ideally happen every few minutes or so. 

 
The batch script below will do what you want. You can have Scheduler run the batch every 5 minutes if you want.

Code:
@echo off
tasklist | findstr -i 7DaysToDieServer.exe

if %errorlevel% EQU 0 goto end
  echo 7D2D Server is not running, starting it...
  cmd /c D:\7 Days to Die Dedicated Server\startdedicated.bat

:end


On my machine (Win 10) for some reason the dedicated server always hangs upon first start (unrelated to the above script; it's always done this). So I get the console window with the first green line of text, but then nothing. The process is running but the server is not actually usable. I have to manually kill the process in taskman and then start it again. The 2nd time it starts up fine. So I'd need to find something much trickier, probably using PowerShell, to get my server started automatically.

 
Just debugging in my head, but does the dedi require something "extra", like a steam 'client', which it would boot up, but fail to connect to on first boot? Just tossing it out here since it crossed my mind..

 
Well, it seems my previous (minimal) troubleshooting was misinterpreted. It seems that although I don't get the full debug output in the console, the server is in fact up and running. So the 1st start attempt only shows this in the console, where normally you'd get a whole bunch of info. But I can connect and play, so I guess my script above would work even for me. But the server almost never crashes, knock-on-wood.

image.png

Edit to add: when in the state above, the console 'shutdown' command does not work. I have to use Task Manager to kill the server.

Edit 2: what it looks line on 2nd attempt:

image.png

 
Last edited by a moderator:
I think rebooting the server once a day would be a good plan, and easily automated.
Yes. If you're on Windows, it advised to do it about every 8 hours. Otherwise you run into memory leak issues. 

On Linux it's typically good for 24 hours.

Linux is simply better overall with better server performance, automation, and fewer general issues. I don't understand why more games aren't developed to run on it.

 
Thanx guys. This script is just the thing I was looking for. I'll give it a try later today. 

As far as server console drawing blank after the "server is starting" line in the console upon first run after a restart - that is a regular occurence for me as well. My workaround is that I start the server, then kill it after about 30 seconds and then just run the starting script again and normally the server console window starts updating then. After I have done it once after the machine restart the next server runs normally update the console log just fine. Fortunately I restart that machine on which the server is running relatively infrequently, every few months or so. 

The batch script below will do what you want. You can have Scheduler run the batch every 5 minutes if you want.

Code:
@echo off
tasklist | findstr -i 7DaysToDieServer.exe

if %errorlevel% EQU 0 goto end
  echo 7D2D Server is not running, starting it...
  cmd /c D:\7 Days to Die Dedicated Server\startdedicated.bat

:end
A question on the script - can the timer for cheking the condition of the server be includuded in the script itself? So that I do not have to include it in the scheduler but can just run the script in the command line or powershell. 

The motivation for that question is that sometimes when I change servers (we from time to time try different mods) it would be mildly annoying if the scheduled script would try to spin up a server while I'm working on configuring it. That way I could just close the CMD in effect stopping the script from running for a little while and when I'm done just open up the command line again and re-run the server cheking script. 

Ofc doing it that way would mean leaving a command line open in the background but that would not be a problem, I presume, on a dedicated server. 

EDIT: The script is not quite working. When I close the server and run the .bat script it first, gives an error message that it cant find 7DaysToDieServer (which is expected ofc) but then tells me that the server is starting, including the 15 sec timer from the main start script but the server itself will not spool up. That is after I added the relevant "..." marks to get around the error message that D:\7 is not a known command because of spaces in the path. 

 
Last edited by a moderator:
A question on the script - can the timer for cheking the condition of the server be includuded in the script itself? So that I do not have to include it in the scheduler but can just run the script in the command line or powershell. 


I suppose you could just put the whole thing in a loop. I'm keeping in this in batch language just cause that's the sample I started from, but really PowerShell would be better. The below script worked for me; only thing I changed were the paths in the 'start' command. Each time the server needs to be started you will get a new Cmd window opened.

Code:
@echo off

:loopstart
  tasklist | findstr -i 7DaysToDieServer.exe

  if %errorlevel% EQU 0 goto running
  
  echo 7D2D Server is not running, starting it...
  start "7 Days To Die Server" /d "d:\7 Days to Die Dedicated Server" "d:\7 Days to Die Dedicated Server\startdedicated.bat"
  REM give it time to get started
  timeout 120
  goto delay
  
:running
echo 7D2D Server is already running...

:delay
REM this is the wait between each check
timeout /t 15 /nobreak
goto loopstart

:end
 
Last edited by a moderator:
it's really a poverty for each OS that you need a handmade script just to check if something is running and if not, restart it …

But i haven't even found a solution for linux, besides running an endless script or a cron job with a custom script.

Maybe i'd should program a gui-configruable app to do so :D

 
Both Windows and Linux support auto-restarting programs, it just takes some special handling. In Windows this means setting options in the properties page of services.msc (and the program must be configured as a service). In Linux I think the recommended way is to configure the program to run as a daemon and add a restart options into its .service file.

 
Both Windows and Linux support auto-restarting programs, it just takes some special handling. In Windows this means setting options in the properties page of services.msc (and the program must be configured as a service). In Linux I think the recommended way is to configure the program to run as a daemon and add a restart options into its .service file.
No, essential difference: They support restarting SERVICES, not regular programs. As you said yourself: You need to run the program as a service, which is a pain in the ass for a regular app itself, too. (Probably less on Linux but a VERY big PITA on Windows, even the built in "runasservice" in Windows is pure @%$#)

However it is in theory not complicated for regular apps to check if they are running and simply restart them if necessary. But for that case, there is no out-of-the-box solution.

 
Last edited by a moderator:
Thanx Boidster - this script you modified indeed works just fine. If the server is running it notes so in the command line, waits the designated time (in seconds) and cheks again. If it is not running it opens a new command line window and starts the startdedicated.bat script and server indeed spins up upon my testing. 

Just the thing I was looking for!

7d2d_script_is_working.jpg

 
Hey, i have a similar question. I want to auto-restart my server every 24h to a specific time (like 6am for example). How is this possible? Im running the server on Win10.
Thanks!

 
Last edited by a moderator:
I suppose you could just put the whole thing in a loop. I'm keeping in this in batch language just cause that's the sample I started from, but really PowerShell would be better. The below script worked for me; only thing I changed were the paths in the 'start' command. Each time the server needs to be started you will get a new Cmd window opened.

Code:
@echo off

:loopstart
  tasklist | findstr -i 7DaysToDieServer.exe

  if %errorlevel% EQU 0 goto running
  
  echo 7D2D Server is not running, starting it...
  start "7 Days To Die Server" /d "d:\7 Days to Die Dedicated Server" "d:\7 Days to Die Dedicated Server\startdedicated.bat"
  REM give it time to get started
  timeout 120
  goto delay
  
:running
echo 7D2D Server is already running...

:delay
REM this is the wait between each check
timeout /t 15 /nobreak
goto loopstart

:end


This worked great for me, along with servertools (to handle the shutdown and many more features) I have an automated shutdown and restart system for our 7d2d dedicated server on windows (in general, not just for crash scenarios).

I just changed the timeout to be longer, 60sec instead of 15, matter of preference is all.

To avoid the extra cmd window for each server restart just edit the original startdedicated.bat that this batch script calls and change "pause" on line 83 to "exit".

 
Last edited by a moderator:
The batch script below will do what you want. You can have Scheduler run the batch every 5 minutes if you want.

Code:
@echo off
tasklist | findstr -i 7DaysToDieServer.exe

if %errorlevel% EQU 0 goto end
  echo 7D2D Server is not running, starting it...
  cmd /c D:\7 Days to Die Dedicated Server\startdedicated.bat

:end


On my machine (Win 10) for some reason the dedicated server always hangs upon first start (unrelated to the above script; it's always done this). So I get the console window with the first green line of text, but then nothing. The process is running but the server is not actually usable. I have to manually kill the process in taskman and then start it again. The 2nd time it starts up fine. So I'd need to find something much trickier, probably using PowerShell, to get my server started automatically.
I spent hours searching for a reusable scriptlet for 7d2d/MC. Thanks friend this saved me much time. :D

 
Hey, i have a similar question. I want to auto-restart my server every 24h to a specific time (like 6am for example). How is this possible? Im running the server on Win10.
Thanks!
EDIT: This is what worked for me having the server start short-cut from steam on my desktop.

To other noobs like me, open note pad up and save this code inside it as (WhateverYouWant.cmd).

Then click it and youre off

:) xx

 

Code:
@echo off

:loopstart
  tasklist | findstr -i 7DaysToDieServer.exe

  if %errorlevel% EQU 0 goto running
  
  echo 7D2D Server is not running, starting it...
  start "7 Days To Die Server" cmd /c "C:\Users\Liamw\Desktop\7 Days to Die Dedicated Server.url"
  REM give it time to get started
  timeout 120
  goto delay
  
:running
echo 7D2D Server is already running...

:delay
REM this is the wait between each check
timeout /t 15 /nobreak
goto loopstart

:end
 
Last edited by a moderator:
Back
Top