So you have a server running on Linux and sometimes it crashes ... some restart automatically, some dont. Here is a little bash script that will keep your server running.
My example server is an Urban Terror server and my start line is this ...
./urt-ffa.i386 +set net_port 27961 +exec server.cfg
To run the server in the background I add an ampersand & at the end. So it doesnt exit when I logout of the SSH I add nohup at the beginning
nohup ./urt-ffa.i386 +set net_port 27961 +exec server.cfg &
So that works until the server crashes. So what I did was create a little script that checks if the server is running every 30 seconds, if not then it starts it back up. This file is called monitor.sh and I start it at the command line with
nohup ./monitor.sh &
I added the nohup and ampersand because I want THIS script to run in the background too, it needs to run after I logout. One benefit of using nohup is that it outputs all the servers output into nohup.out so you have a log file.
#!/bin/bash cd /home/frank/urt/ffa # start server nohup ./urt-ffa.i386 +set net_port 27961 +exec server.cfg & while true do sleep 30s if ps -ef | grep -v grep | grep urt-ffa.i386 >/dev/null then date > lastchecked.txt else date >> lastcrashed.txt nohup ./urt-ffa.i386 +set net_port 27961 +exec server.cfg & fi done