[Release] Server Auto Restart

Hey guys!

I know a lot of you guys would have knew this already.
This is for you novice server hosts out there who are looking for a simple .bat script to auto restart your server every hour with a 30 secs pause in between the restart.

All you gotta do is replace the info in the original run.bat file and replace with the code below:

:loop
start CitizenMP.Server.exe %*
timeout /t 3600 >null
taskkill /f /im CitizenMP.Server.exe >nul
timeout /t 30 >null
goto loop```



And that's it.

Very simple but very handy.

Hope it helps.
8 Likes

Don’t forget bash script! :wink:

#!/bin/bash
while :
do
echo starting server!
screen -d -m mono CitizenMP.Server.exe %
sleep 1h
pkill screen
echo Sleeping for 30s before next restart…
sleep 30s
clear
done

5 Likes

@exalented good call! Forgot about that.

Hi,
Why would I need to restart the server every hour? And thanks for the bat script.

@COndor my server used to be very busy and with people spawning things the server would really struggle so a hour restart was necessary back then. But I have now upgraded my server hosting.

But you can change the time. I now auto restart every 4 hours. The bat scripting works in seconds.
So change 3600 to 14400 for 4 hours.

i think its not the best solution because you cant view the console. I have a script that starts a wacher process wich calls a script wich stops and starts the server via a screen command at a specific time ( set in the first script to have it simple ). May sound confusing but in the end its very simple.

Means you can always "screen -r " and see whats going on if needed.
I also can manualy start / stop / restart the server with my script.

EDIT
I dont saw that the first script is made for windows. And the second (bash) script uses screen.
Sorry for that.

@exalented if you interested i could release it

screen

@Slluxx Sure, If you want to, I’m sure many people would appreciate it. :slight_smile:

@exalented yeah, well, i have to modify it and i probably can only release a small version 4 free and for a donation you could get full acces or so. Im doing everything 4 free atm without a job :smiley:

@frugoo This is awesome, i am new to most of this, but i’m curious is it possible to add to this to make the .bat auto delete the cache file in your reborn server directory when the 30 sec pause is activated so when the server starts it is fresh???

RMDIR /S /Q “PATH\TO\CACHE\FOLDER”

Start with Thank You for helping me, but the annoying question and i’m sorry but where would this RMDIR /S /Q “PATH\TO\CACHE\FOLDER” go in the .bat

thats the command to delete the cache folder. a batch file works from top to bottom so you should insert that before some kind of “restart” line - or after the server gets killed.

1 Like

Thank You, i did as said and it works perfect. Deletes the cache at startup and restart.

1 Like

I made a slight alteration to this script to allow it to delete cache, remember setting the initial path and or proper paths is important whenever deleting files in a batch file less you run the script as adminstrator and it delete files by default in C:\windows\system32!!!

@echo off
SETLOCAL
set CFXDIR = %CD%
:loop
cd %CFXDIR%
rd /s /q cache
start /wait CitizenMP.Server.exe %*
timeout /t 3600 >null
taskkill /f /im CitizenMP.Server.exe >nul
timeout /t 30 >null
goto loop
ENDLOCAL

REM (updated script) for wait using windows start function.

It is possible to restart the loaded scripts when changing a file?
So I don’t need to type restart ScriptName every time I want to try something out
(Only for debugging and developing)

I guess you could make it send the “restart Resource” command via rcon somehow as soon as you save if your editor supports doing actions on save. Otherwise I don’t think it’s possible right now unfortunately.

You can’t reload scripts without things being buggy as the cache files need to be updated (example try editing one the server files while the server is running it will start throwing exceptions.

But you can restart the server easily with this script.

just type in any cmd window

taskkill /f /im timeout

in 30 seconds your server will restart or if you need it immeditly type it twice:

taskkill /f /im timeout
taskkill /f /im timeout

-> the script is set to to have two timeouts for your server/system safety but you could also remove the timeout lines.

additionally you should modify the script so that it has start /wait CitizenMP.Server %* for wait otherwise it will restart your sever every 3600 + 30 seconds not just when it fails. I forgot to update that in my edit.

I’m confused as to what exactly you mean…

Do I need to replace any of the text you listed?

Thanks

is there a way to send /say to server from bat?

this method is old now and pretty much obsolete. As restart (resource) is now a function. So is not recommended any more.
It may be possible. But I wouldn’t know how without looking into it a bit more.