|
Linux init script to start server on boot
|
| Author |
Message |
scriptfu
Junior Member
 
Posts: 15
Group: Registered
Joined: Dec 2007
Status:
Offline
|
Linux init script to start server on boot
Hi everyone,
I'm new to the SRCDS scene, and I have just set up my first dedicated TF2 server on linux. This website has been a great source of information for me setting everything up, so I wanted to give a little back.
I found just a few init scripts around the internet, though nothing really did what I wanted it to do, so I made my own. I like my init scripts to tell me more information that a simple "Service started" message. I also wanted the ability to stop and even restart a service without much effort in case the server failed.
This script is rather simple to configure and should only take you 10 minutes to set up. Configure the script's variables at the beginning to match your server's settings. It's well commented to help you understand what everything does.
#!/bin/sh
# Source Dedicated Server Init Script
# Server options
TITLE='Source Dedicated Server' # Script initialization title
LONGNAME='Team Fortress 2' # Full title of game type
NAME='tf2' # Server handle for the screen session
DAEMON='srcds_run' # The server daemon
STEAM='/home/tf2/orangebox' # STEAM to Steam installation
USER='tf2'
# Game options
IP='72.52.248.250' # IP of the server
PORT='27015' # Port number to
MAP='ctf_2fort' # Initial map to start
GAME='tf' # Game type (tf|cstrike|valve|hl2mp)
SIZE='24' # Maximum number of players
# Server options string
OPTS="-game $GAME +hostname \"$CLIENT\" +map $MAP +ip $IP -port $PORT \
-autoupdate +maxplayers $SIZE -pidfile $STEAM/$GAME/$NAME.pid"
# Screen command
INTERFACE="/usr/bin/screen -A -m -d -S $NAME"
service_start() {
# Check if the pid files currently exist
if [ ! -f $STEAM/$GAME/$NAME.pid ] && [ ! -f $STEAM/$GAME/$NAME-screen.pid ]; then
if [ -x $STEAM/$DAEMON ]; then
echo "Starting $TITLE - $LONGNAME"
echo "Server IP: $IP"
echo "Server port: $PORT"
echo "Server size: $SIZE players"
cd $STEAM
$INTERFACE $STEAM/$DAEMON $OPTS
# Prevent race condition on SMP kernels
sleep 1
# Find and write current process id of the screen process
ps -ef | grep SCREEN | grep "$NAME" | grep -v grep | awk '{ print $2}' > $STEAM/$GAME/$NAME-screen.pid
echo "$TITLE screen process ID written to $STEAM/$GAME/$NAME-screen.pid"
echo "$TITLE server process ID written to $STEAM/$GAME/$NAME.pid"
echo "$TITLE started."
fi
else
echo -e "Cannot start $TITLE. Server is already running."
#exit 1
fi
}
service_stop() {
if [ -f $STEAM/$GAME/$NAME.pid ] && [ -f $STEAM/$GAME/$NAME-screen.pid ]; then
echo "Stopping $TITLE - $LONGNAME."
# Get the process ID from the pid file we created earlier
for id in `cat $STEAM/$GAME/$NAME-screen.pid`
do kill -9 $id
echo "Killing process ID $id"
echo "Removing $TITLE screen pid file"
rm -rf $STEAM/$GAME/$NAME-screen.pid
break
done
# Remove server pid file
echo "Removing $TITLE pid file"
rm -rf $STEAM/$GAME/$NAME.pid
# Wipe all old screen sessions
screen -wipe 1> /dev/null 2> /dev/null
echo "$TITLE stopped."
else
echo -e "Cannot stop $TITLE. Server is not running."
#exit 1
fi
}
case "$1" in
'start')
service_start
;;
'stop')
service_stop
;;
'restart')
service_stop
sleep 1
service_start
;;
*)
echo "Usage $0 start|stop|restart"
esac
On Debian, add this script to your /etc/init.d directory.
sudo update-rc.d <script> defaults
To start the server:
/etc/init.d/<script> start
To stop the server:
/etc/init.d/<script> stop
To restart:
/etc/init.d/<script> restart
Please let me know if you have any questions or need help setting this up.
Cheers![/code]
Dan
=-=-=-=-=-=-=-=-=
|
|
| 12-08-2007 06:01 PM |
|
 |
Spartanfrog
Super Moderator
     
Posts: 1,987
Group: Super Moderators
Joined: May 2007
Status:
Offline
Reputation: 0
|
RE: Linux init script to start server on boot
Its pretty cool. You should have put in the tutorial section though i think. It is linux so who knows lol.
Has someone helped you on these forums? If so, help someone else
OrangeBox is a WHORE.
|
|
| 12-09-2007 04:09 AM |
|
 |
scriptfu
Junior Member
 
Posts: 15
Group: Registered
Joined: Dec 2007
Status:
Offline
|
RE: Linux init script to start server on boot
@ SpartanFrog,
Good point I'll add some more instructions, format it a little more nicely, and add it to the tutorials section. My second edition of the script will take a second command-line parameter to start, stop and restart multiple game servers on the same physical server.
I like scripting (as my name implies), so if anyone has requests for a script, please post here or PM me. I'd prefer a post so others can benefit.
Cheers!
Dan
=-=-=-=-=-=-=-=-=
|
|
| 12-09-2007 05:02 PM |
|
 |
jameshaigh
Member
  
Posts: 99
Group: Registered
Joined: Jun 2007
Status:
Offline
|
|
| 12-09-2007 09:44 PM |
|
 |
scriptfu
Junior Member
 
Posts: 15
Group: Registered
Joined: Dec 2007
Status:
Offline
|
RE: Linux init script to start server on boot
Hi everyone,
It's always a good idea to run any service as a user instead of root, so I'll be modifying this script to run as a user. Would it be beneficial to anyone if I wrote a tutorial on setting up a chrooted SRCDS environment? I have seen many tutorials on setting up SRCDS, but none yet on setting up SRCDS in a secure manner.
Cheers,
Dan
=-=-=-=-=-=-=-=-=
|
|
| 12-10-2007 08:02 PM |
|
 |
Nethouse2000.gl
Junior Member
 
Posts: 14
Group: Registered
Joined: Mar 2007
Status:
Offline
|
RE: Linux init script to start server on boot
IŽd love a walkthrough script for centos 4.2 
I have been using screen to start stop servers, but would like a more automated manner 
And yes, securing the box for dummies like me would be great!
|
|
| 12-11-2007 10:36 AM |
|
 |
HBS|Ryan
HotBettyServers.com
   
Posts: 441
Group: Registered
Joined: Mar 2007
Status:
Offline
|
RE: Linux init script to start server on boot
Hi everyone,
It's always a good idea to run any service as a user instead of root, so I'll be modifying this script to run as a user. Would it be beneficial to anyone if I wrote a tutorial on setting up a chrooted SRCDS environment? I have seen many tutorials on setting up SRCDS, but none yet on setting up SRCDS in a secure manner.
Cheers,
Dan
that would be great!
The Very Best in West Coast CSS
1000fps CSS Servers
http://www.HotBettyServers.com
|
|
| 12-11-2007 12:50 PM |
|
 |
scriptfu
Junior Member
 
Posts: 15
Group: Registered
Joined: Dec 2007
Status:
Offline
|
RE: Linux init script to start server on boot
@Nethouse2000.gl:
I'll be putting CentOS 5.1 on my latest server. I'm a Debian fan personally, but CentOS/RedHat are both more popular. Once I get everything up and running, I'd be happy to write a tutorial for both Debian and Red Hat-based distributions. I will gladly accept requests if anyone uses something different (unless it's Gentoo, because you should be able to do it yourself ).
The CentOS tutorial should work for versions 4 and up; there is little difference with configuring chkconfig, et alia.
On another note, I have been looking for a web-based SRCDS administration toolkit, and haven't found one yet. Is there such a thing? I would like to be able to edit various files, like my server.cfg, maplist, mapcycle, etc.
Cheers!
=-=-=-=-=-=-=-=-=
|
|
| 12-11-2007 05:42 PM |
|
 |
VRDriver4l
Balance Point
  
Posts: 71
Group: Registered
Joined: Jan 2006
Status:
Offline
|
RE: Linux init script to start server on boot
@Nethouse2000.gl:
I'll be putting CentOS 5.1 on my latest server. I'm a Debian fan personally, but CentOS/RedHat are both more popular. Once I get everything up and running, I'd be happy to write a tutorial for both Debian and Red Hat-based distributions. I will gladly accept requests if anyone uses something different (unless it's Gentoo, because you should be able to do it yourself  ).
The CentOS tutorial should work for versions 4 and up; there is little difference with configuring chkconfig, et alia.
On another note, I have been looking for a web-based SRCDS administration toolkit, and haven't found one yet. Is there such a thing? I would like to be able to edit various files, like my server.cfg, maplist, mapcycle, etc.
Cheers!
I use IGC game panel on my clan box and it does the job. 5 bucks a month and does everything you need to be done through a web based control. Good luck.
http://www.innovativegamingconcepts.com/
|
|
| 12-11-2007 11:08 PM |
|
 |
trev-gdr
Junior Member
 
Posts: 24
Group: Registered
Joined: Dec 2007
Status:
Offline
|
RE: Linux init script to start server on boot
@Nethouse2000.gl:
I'll be putting CentOS 5.1 on my latest server. I'm a Debian fan personally, but CentOS/RedHat are both more popular. Once I get everything up and running, I'd be happy to write a tutorial for both Debian and Red Hat-based distributions. I will gladly accept requests if anyone uses something different (unless it's Gentoo, because you should be able to do it yourself  ).
The CentOS tutorial should work for versions 4 and up; there is little difference with configuring chkconfig, et alia.
On another note, I have been looking for a web-based SRCDS administration toolkit, and haven't found one yet. Is there such a thing? I would like to be able to edit various files, like my server.cfg, maplist, mapcycle, etc.
Cheers!
I use IGC game panel on my clan box and it does the job. 5 bucks a month and does everything you need to be done through a web based control. Good luck.
http://www.innovativegamingconcepts.com/
Same panel I also use. Can be a bitch to setup on debian-based linux distros, but it runs well once you get the hang of it .
|
|
| 12-12-2007 02:31 AM |
|
 |
scriptfu
Junior Member
 
Posts: 15
Group: Registered
Joined: Dec 2007
Status:
Offline
|
RE: Linux init script to start server on boot
I tried to test out their demo, but it threw MySQL errors. It may be worth the $5 to check it out.
=-=-=-=-=-=-=-=-=
|
|
| 12-12-2007 03:12 AM |
|
 |
Spartanfrog
Super Moderator
     
Posts: 1,987
Group: Super Moderators
Joined: May 2007
Status:
Offline
Reputation: 0
|
RE: Linux init script to start server on boot
tcadmin.com More proffesional but its good
Has someone helped you on these forums? If so, help someone else
OrangeBox is a WHORE.
|
|
| 12-12-2007 06:29 AM |
|
 |
scriptfu
Junior Member
 
Posts: 15
Group: Registered
Joined: Dec 2007
Status:
Offline
|
RE: Linux init script to start server on boot
Hmm, I only host one server for now, and it's not even populated 90% of the time I'm surprised there's no open source one out there. Heck, I think I just found my next project 
Cheers,
Dan
=-=-=-=-=-=-=-=-=
|
|
| 12-12-2007 07:50 PM |
|
 |
trev-gdr
Junior Member
 
Posts: 24
Group: Registered
Joined: Dec 2007
Status:
Offline
|
RE: Linux init script to start server on boot
tcadmin.com More proffesional but its good
Doubt he uses windows since he is coding a linux script 
The only that I know is close to free (not necassarily opensource) is the cpanel one, but that's only if you already have cpanel installed on your box. http://cpgs.cpanel.net/
|
|
| 12-13-2007 04:01 AM |
|
 |
Spartanfrog
Super Moderator
     
Posts: 1,987
Group: Super Moderators
Joined: May 2007
Status:
Offline
Reputation: 0
|
RE: Linux init script to start server on boot
Yeah your right lol. I know muppet was looking for beta testers for a control panel. Shoot him a PM.
Has someone helped you on these forums? If so, help someone else
OrangeBox is a WHORE.
|
|
| 12-13-2007 06:00 AM |
|
 |
|
|