linux is the operating system of choice when using this plugin.
Once all players disconnect from the server, this plugin will either:
1) (default) Reset the server game mode, map, & player slots to the values found on first bootup, or
2) Shut down the server after all players DC.
Other games can use the server command "_restart" to restart their game engine but it doesn't work on L4D2, so that's why we set up the configuration in this way and added the option for a hard restart.
cvars
Spoiler
// This file was auto-generated by SourceMod (v1.9.0.6252)
// ConVars for plugin "plugin.smx"
// Shut down the server when empty? (Don't enable unless you know how to set up a cron job script to reboot the game server.)
// 1 = true, 0 = false (default)
// -
// Default: "0"
// Minimum: "0.000000"
// Maximum: "1.000000"
sm_Hard_Restart_Enabled "0"
// Reset player slots (sv_maxplayers) on map reset?
// (Incase an admin or plugin changes slots and doesn't set it back - this simply resets it back to the value found when the server first launched.)
// 1 = true (default), 0 = false
// -
// Default: "1"
// Minimum: "0.000000"
// Maximum: "1.000000"
sm_reset_player_slots "1"
Recommended cron job & auto-restart.sh script
(Use if sm_Hard_Restart_Enabled is set to "1")
Cron job:
Spoiler
Code:
# Check every minute to see if the server is down
* * * * * /path/to/auto-restart.sh
auto-restart.sh
Spoiler
Note:
* Edit the script accordingly, instructions are detailed.
* This script can reboot multiple game servers on the same machine.
* Use a Linux editor such as nano (don't create the script in Windows and upload it).
PHP Code:
#!/bin/bash
# ############################### # ## ~About / How to use this~ # # ############################### # # auto-restart.sh # # This script is intended to be launched by a cronjob task every minute # to check for any offline game servers. The cronjob would look like this: # * * * * * /path/to/auto-restart.sh # # (this script assumes every game server has it's own startup script and a unique screen name) # # You can add as many servers as you like. HOWEVER, keep the name the same # BEFORE the equal sign. e.g. server1, server2, server3. & start_svr1, start_svr2, # start_svr3. # # Screen names should be unique enough that the full name of one server isn't found in another. # e.g. L4D2_svr1 & L4D2_svr2 would be OK, but this would be problematic: L4D2_svr & L4D2_svr2 # # EXAMPLE customization: # NUM_SERVERS=3 # # server1=L4D2_svr1 // screen names, assigned in startup script ("screen -mdS L4D2_svr1") # server2=L4D2_svr2 # server3=CSGO_svr # # start_svr1=/path/to/l4d2svr1.sh // path to each server's start (bootup) script # start_svr2=/path/to/l4d2svr2.sh # start_svr3=/path/to/CSGOsvr.sh
# The three checks before launching a game server are because the "alive" variable can sometimes incorrectly return a value of 1 (no server detected) # https://i.imgur.com/UDsFTil.png so it would relaunch the game server when it's already running...
############################# # Customization starts HERE # #############################
# How many game servers on your machine? NUM_SERVERS=2
########################################### # Edit your game server screen names HERE # ########################################### #note: DON'T edit before the equal sign
server1=left4dead2_server server2=CSGO_server
#server3= #screen name for server 3 #server4= #screen name for server 4 #etc..
#################################### # Edit path to launch scripts HERE # #################################### #note: DON'T edit before the equal sign
if [ $alive -lt 2 ]; then echo "Check #1: Game server offline." sleep 3s
alive2=`ps ux | grep "${!var}" | wc -l | awk '{ print $1 }'` if [ $alive2 -lt 2 ]; then echo "Check #2: Game server still offline." sleep 3s
alive3=`ps ux | grep "${!var}" | wc -l | awk '{ print $1 }'` if [ $alive3 -lt 2 ]; then echo "Check #3: Starting game server..." sleep 3s ${!var2} else echo "Third Check: game server already active. Skipping.." sleep 3s fi else echo "Second Check: game server already active. Skipping.." sleep 3s fi else echo "First Check: game server already active. Skipping.." sleep 3s fi
i=$((i+1));
echo "" done exit
auto-restart.sh V2 (more features, tested only on Ubuntu 18.04)
Spoiler
Note:
* This script can reboot multiple game servers on the same machine.
* Use a Linux editor such as nano (don't create the script in Windows and upload it).
* Added functionality: auto-update game server, auto-update custom maps, full system reboot @3am if all servers found empty
* Example of all fields filled out for 2 game servers: https://onlinegdb.com/H1NuX3Yi7
* In case pasting to nano editor gets buggy, you can directly download the file to your linux machine:
####################### # auto-restart.sh # This script should be launched every minute from a cron job task # & should be run from the same location steam is installed. # # It's mainly for lazy admins who prefer to have their servers & custom maps updated automatically, # and who also likely have in-game sourcemod plugins installed which shut down # the game server after certain events (e.g. all players DC from server ) # # Session Names should be unique, one shouldn't overlap entirely from another. # e.g. L4D2_server & L4D2_server2 would be problematic. # # ========================================================================== # You should manually test the following things before launching this script: # # Auto-update functionality: # "screen --help", make sure the "-logfile" option is listed, otherwise set EnableUpdateCheck to 'no' # The -logfile screen option will be in later versions of screen (e.g. 4.06.02) # # Automatically update any workshop maps before server launch: # Download here: https://github.com/nosoop/steam_workshop_downloader # Example: ./workshop.py -o path/to/addons/workshop collectionID # If you want this disabled just leave the workshop fields blank. # Make sure your workshop folder is installed within the addons folder before using this. # # Full system reboot @ 3am if all game servers are empty (can disable by setting RebootAt3am to 'no'): # Download here: https://github.com/dustinandband/Empty-Server-Checker # Run command "python IsServerEmpty.py ip port rconpass" # (Ignore warning about no handlers being found. I modified hustl4's script and am new to python so it'll be fixed in future.) # Need to install pip (module manager for python) and python-vpk before using this: # apt-get install python-pip # pip install python-valve # # For more convinient downloading~ # curl -o workshop.py 'https://raw.githubusercontent.com/nosoop/steam_workshop_downloader/commed-patch/workshop.py' # curl -o IsServerEmpty.py 'https://raw.githubusercontent.com/dustinandband/Empty-Server-Checker/master/IsServerEmpty.py' # #######################
#Allow auto-update check? #Not recommended if your update script triggers steam guard authentication, or your version of screen doesn't include the -logfile option. #yes or no EnableUpdateCheck=( '' '' )
#Enable full system reboot @3am (if all servers are detected as empty)? #yes or no RebootAt3am=
ServerIP=xxx.xxx.xxx.xxx
ServerPort=( '' '' )
# Needed for 3am reboot function # If disabled enter random text here so the 'Check_Input' test will pass RconPassword=( '' '' )
#optional #Need to manually create the workshop folder first WorkShopDirectory=( '' '' )
#optional WorkshopCollection=( '' '' )
# Customization ends here
scriptcheck=`pgrep -c auto-restart.sh` if [ $scriptcheck -gt 1 ]; then echo "auto-restart.sh script already in use. Terminating.." exit fi
if [ $alive -lt 2 ] && [ $alive2 -lt 2 ] && [ $alive3 -lt 2 ]; then #Server's definitely offline. Check for any workshop updates before launching server echo "Server is offline. Checking for updates before launching server.." if [ -n "${WorkShopDirectory[$j]}" ] && [ -n "${WorkshopCollection[$j]}" ]; then if [ ! -e "workshop.py" ]; then # This version of the workshop DLer caps failed downloads at 25, # so it won't get hung up forever if valve server is down.. curl -o workshop.py 'https://raw.githubusercontent.com/nosoop/steam_workshop_downloader/commed-patch/workshop.py' chmod +x workshop.py fi ./workshop.py -o "${WorkShopDirectory[$j]}" "${WorkshopCollection[$j]}" fi #auto-update check if [ -e ${SessionName[$j]}.output ] && [ ${EnableUpdateCheck[$j]} = "yes" ]; then #check for the word 'MasterRequestRestart' from last screen session - which means we should update if [ $(grep -c "MasterRequestRestart" "${SessionName[$j]}.output") -gt 0 ]; then echo "Update Needed, Updating server.." ${UpdateScript[$j]} fi #Remove previous screen session log so it doesn't keep finding that word from past sessions. rm ${SessionName[$j]}.output fi # Finally, launch server if [ ${EnableUpdateCheck[$j]} = "yes" ]; then screen -mdSL ${SessionName[$j]} -Logfile ${SessionName[$j]}.output ${srcds_run[$j]} ${Parameters[$j]} else screen -mdS ${SessionName[$j]} ${srcds_run[$j]} ${Parameters[$j]} fi echo "${SessionName[$j]} is now running." else echo "Server already online. Skipping.." fi done echo "Server Check Complete" }
Check_Input() { if [ -z "${num_of_servers}" ] || [ -z "${ServerIP}" ] || [ -z "${RebootAt3am}" ]; then echo "Error: One of following left blank: 'num_of_servers', 'ServerIP', 'RebootAt3am'." exit 1 fi
#Make sure all non-optional fields are completed. typeset -n x for x in SessionName srcds_run Parameters UpdateScript EnableUpdateCheck ServerPort RconPassword; do for((j=0;j<$num_of_servers;j++)); do if [ -z "${x[$j]}" ]; then echo "Error: One or more non-optional items left blank." exit 1 fi done done echo "Initialization Check successfull." } RebootAt3AM() { if [ ! -e "IsServerEmpty.py" ]; then curl -o IsServerEmpty.py 'https://raw.githubusercontent.com/dustinandband/Empty-Server-Checker/master/IsServerEmpty.py' chmod +x IsServerEmpty.py fi EmptyCount=0 # Empty server check for ((j=0;j<$num_of_servers;j++)); do Result=$(python IsServerEmpty.py $ServerIP ${ServerPort[$j]} ${RconPassword[$j]}) if [[ $Result =~ "server is empty" ]] || [[ $Result =~ "Server is offline" ]]; then sleep 10s #check one more time JUST TO BE SURE Result2=$(python IsServerEmpty.py $ServerIP ${ServerPort[$j]} ${RconPassword[$j]}) if [[ $Result2 =~ "server is empty" ]] || [[ $Result2 =~ "Server is offline" ]]; then # Quit screen session and increment count. # Ignore "No screen session found." - happpens when u execute this command if server was already offline. screen -r ${SessionName[$j]} -X quit ((EmptyCount++)) fi else echo "${SessionName[$j]} is not empty or offline." fi done if [ $EmptyCount -eq $num_of_servers ];then sleep 40s #To prevent relaunching the server @ 3am /sbin/shutdown -r now fi }
# Function calls Time_Var=$(date +%H%M) if [ $Time_Var -eq 0300 ] && [ $RebootAt3am = "yes" ]; then # since cron launches this every min, this'll get executed @ 3am every morning. RebootAt3AM else Check_Input Launch_Any_Offline_Servers fi
Install
Put .smx file in addons/sourcemod/plugins
The configuration file is generated automatically - > cfg/sourcemod/ResetWhenEmpty.cfg
Changelog
- Oct 21, 2018 (auto-restart.sh V2):
Spoiler
Update:
auto-restart.sh V2 added
* Added functionality: auto-update game server, auto-update custom maps, full system reboot @3am if all servers found empty
* Example of all fields completed for 2 game servers: https://onlinegdb.com/H1NuX3Yi7
* Instructions are detailed, shouldn't have to rewrite them here
- Oct 15, 2018 (resetonempty_v2):
Spoiler
Update:
version 2.0
* Added two additional 10 second checks when server's detected as empty - in case someone DC's during map change.
* If someone joins in within this time frame they might see a chat printout about a cvar change. Thought this was real minor so I didn't do anything about it.
- Oct 12, 2018 (version 1.0):
version 1.0
Spoiler
Update:
* Rewritten the plugin entirely.
* Added functionality to the auto-restart script.
Notes
* This plugin should work in most (or all) games. It's tested in CS:GO and L4D2.
* The auto-restart script can reboot any amount of game servers (across varying games). Let me know if the instructions are clear or too vague. I tried to be as clear as possible.
* The previous plugin (and other similar plugins) had a timer that executed every x amount of minutes and shut down the server if empty - to keep it refreshed. It's been removed since timer callbacks don't get executed when the server is in hybernation mode.
TODO:
During map transition (from OnMapEnd until a few seconds after OnMapStart) client count will return '0' even when there's players on the server. If a client DC's during that time the server might shut down. Working on fix. (Checking client count during map change was an afterthought after uploading this version of the plugin.)
Last edited by dustinandband; 09-16-2019 at 02:42.
Reason: github link
Update:
* modified auto-restart.sh script to be able to launch multiple L4D2 servers on the same machine.
The three checks before launching a game server are because the "alive" variable can sometimes return a value of 1 when there's an active server up, though very brief: https://i.imgur.com/UDsFTil.png
Last edited by dustinandband; 04-05-2018 at 03:21.
Update:
* modified auto-restart.sh script to be able to launch multiple L4D2 servers on the same machine.
The three checks before launching a game server are because the "alive" variable can sometimes return a value of 1 when there's an active server up, though very brief: https://i.imgur.com/UDsFTil.png
can't beleive this plugins exist, thanks a bunch man