So, I wanted to create a script that could check if a server was empty and not running the default map and restart that server. This is it: (depends on gamedig being installed globally)
Code:
#!/bin/bash
gameserver="l4d2server"
game="left4dead2"
config_path="./lgsm/config-lgsm/$gameserver/"
server="1.1.1.1" #change to your ip
for i in $(find . -maxdepth 1 -name "$gameserver*" | sed 's|^./||'); do
defaultmap=$(grep -P '^(?=[\s]*+[^#])[^#]*(defaultmap)' $config_path$i.cfg | awk -F\" '{print $2}')
port=$(grep -P '^(?=[\s]*+[^#])[^#]*(port)' $config_path$i.cfg | grep -v 'client' | awk -F\" '{print $2}')
currentmap=$(gamedig --type $game $server:$port | jq '.map' | awk -F\" '{print $2}')
players=$(gamedig --type $game $server:$port | jq '.players | length')
if [[ "$currentmap" != "$defaultmap" && $players -eq 0 ]]; then
./$i restart
fi
done
- Change server ip on line 5.
- Save to your server directory as an sh file e.g. check.sh
- Run crontab -e and paste this to run every 5 minutes: */5 * * * * /home/l4d2server/check.sh > /dev/null 2>&1
- Lastly, if you haven't already run npm install gamedig -g