Skip to content

Access a website in bash and react to it

nolf2Sometimes specific scripts I wrote run for years and suddenly I realize, that essential parts are missing in them, which breaks everything.

I wrote the article Host Windows games on a Linux server a while ago – the setup described in this article is running on this machine since a lot of years. A bash script is checking if the process of a gameserver is running (it doesn’t matter if it’s a wine process or a native one). If it doesn’t run, it will be started. This ensures that all my game servers are available for 99% of the year.

If a program is written fairly well, this isn’t even a problem. When a fault happens, these ones kill themselves and get restarted after some seconds.

The server executable of “No One Lives Forever 2” was a bit more difficult. When I started it, people could play on the machine for some hours straight, until it lost the connection to the internet. The .exe was still running, so there was no way for my bash script to react to it.

But on this adress I have a web frontend running, which is displaying to me very reliably if the NOLF2 server is still running or not.

First I thought, I would only have to check if the phrase “OFFLINE” is appearing on that website and if it is, kill the server and restart it. This idea was very dumb, because when a player gives himself the name “OFFLINE” he can kill the server with that. Also, the program needs some time to load up the maps and the web interface is caching requests for some seconds, too.

I ended up with this solution – this script is checking if the server is online. If it is, it immediately aborts. When the server is offline, the scripts checks for the status again after 90 seconds. That’s simple enough to make it accessible most of the time:

#!/bin/bash
# if online is NULL, it's down. May change maps, so wait and check again:
if [ -z "$(curl -sSf http://hirnschwund.net/?s=4 | grep "ONLINE")"]; then
echo "Shit is offline"
sleep 90
fi
if [ -z "$(curl -sSf http://hirnschwund.net/?s=4 | grep "ONLINE")"]; then
echo "Shit is still offline"
echo "Killing process"
pkill NOLF2Srv.exe
sleep 2
echo "Starting server"
nohup xvfb-run -n 666 wine NOLF2Srv.exe >/dev/null 2>/dev/null &
else
echo "NOLF2 is running, leave."
fi

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top