You can use bash scripts to automatically start and restart your servers.
Basic server start script
This is an example of a start script I use to start a server. Instead of having to type in a really long server command line
, all I have to do is run the script.
First, create a bash script within the server's parent directory. For a GO server, this would be in, for example, /user/GO. (Within GO would be directories like /bin or /csgo.)
To keep things simple, I named all my start scripts alphabetically.
screen -X -S alpha quit
/usr/bin/screen -dmS alpha ./srcds_run -game csgo +game_type 0 +game_mode 1 -authkey ############+host_workshop_collection 125645871 +workshop_start_map 125498851 -ip 22.214.171.124 +port 27018 -usercon -nowatchdog -auutoupdate -tickrate 128 -nohltv -autoupdate -steam_dir /home/gamer/go/steamcmd -steamcmd_script server1.txt
Line 1 tells the server that I'm running a bash script.
Line 2 kills any previous screen sessions
that might be running. (Notice, to keep it simple, I named my screen alpha since the alpha script is starting it.
Line 3 allows it to sleep for 4 seconds, just to make sure all the processes are dead.
Line 4 moves to the correct directory in which to start the server. (I had to add this line for the script to executable from a "parent script" that I use to restart all the servers at once. More on that in a minute.)
Line 5 starts my server in a detached screen with the name alpha.
Now I want to make sure the script is executable.
chmod +x alpha.sh
Now, if I want to start the server, I can just go into the /home/gamer/go/server1/ directory and type ./alpha.sh. This will automatically start the server.
Creating a "parent" restart script
Since I run a lot of servers, it's really nice to be able to just run one script to start all my servers instead of having to go through a whole bunch of directories starting a whole bunch of different scripts. So I made this script in my user's home directory.
Note: lines started # are comments, meaning those scripts won't be run. It's a really easy way to disable or enable servers as needed. Don't forget to change this script to executable!
If I want to be able to manually restart all my servers at once, for example after updating them all, I just have to go to /home/gamer and type in ./start2.sh.
Getting them to start automatically
I can add a line in crontab so these will start automatically if the server reboots. I've also provided an example of how to restart the scripts every morning at 6am.
@reboot gamer /home/gamer/start2.sh
00 6 * * 0,2,3,4,5,6 amrestart /home/gamer/start2.sh