I have a hobby server that I’m deploying to a digital ocean droplet. I run this server as any other program and it does what it was programmed to do:
1
./myserver
The problem is that this server is not perfect and I’m OK with that. Nevertheless, I don’t want to have to restart it manually every time it dies. For that reason I did some googling and found an easy way to restart my server if it unexpectedly dies:
1
2
3
4
5
6
7
8
#!/usr/bin/env bash
until /home/tacos/myserver >> myserver.log 2>> myserver.error.log; do
echo "$(date -u) - Server crashed with exit code $?. Respawning..." >> runner.log
sleep 1
done
echo "$(date -u) - Server manually killed" >> runner.log
This takes care of restarting the server if it dies, and it also logs a message to a file called runner.log so you can see when the server died or when it was manually killed.
Another important thing we should take care of, is what happens if the computer is restarted. We don’t want to have to manually start this script every time, so we should automate the start of this script. The easiest way I found is to use crontab.
1
crontab -e
And then add this line:
1
@reboot /home/tacos/run-myserver.sh
Now you have a script that will run on that machine all the time, unless you manually stop it.
automation
linux
projects
server
]