Linux Start/Stop/Restart Script

Any questions regarding getting Jaymod running.
User avatar
Chester
Jaymod Developer
Posts: 985
Joined: Sun Apr 24, 2005 7:52 pm
Location: Above the 49th Parallel.
Contact:

Linux Start/Stop/Restart Script

Post by Chester » Fri Jun 16, 2006 12:27 am

Code: Select all

#!/bin/sh

# This belongs in /etc/rc.d/init.d
# You can auto start this by symlinking in one of the startup dirs, ie. startup daemon
# usage: ./nameofthisfile start
# usage: ./nameofthisfile stop
# usage: ./nameofthisfile restart
# same usage as etadmin_mod
# this can be used for ANY program, not just ET.
# rename this file to make unique cases along with edited values below
# this does not have to be shell connected to any user to work. 
#Any user can run it with proper permissions
# This script can also be changed to a screen command by editing 
# the appropriate lines that are commented out, and
# commenting the line following in the start section.
			# screen -d -m su - \ -uncomment this line below
			# '$SERVER_BIN' $SERVER_OPTIONS -uncomment this line below
			# $SERVER_BIN $SERVER_OPTIONS - comment this line
# Edit values as needed

# startup script for your server
SERVER_OPTIONS="+set net_port 27962 +set net_ip 66.333.333.333 +set dedicated 2 +set fs_homepath /home/etpro +set fs_basepath /home/etpro +set com_hunkmegs 128 +set ttycon 0 +set fs_game etpro +exec server.cfg"

# full path to your executable file.
SERVER_BIN=/home/wolfet/etded.x86

# This is where PID are stored
# change the etpro to a unique name. ie /tmp/jaymod_server_pid
SERVER_PIDFILE=/tmp/etpro_server_pid
SCRIPT_PIDFILE=/tmp/etpro_script_pid

# the directory your game is found in
SERVER_ROOT=/home/wolfet/

# the username for the account that the server should be run as
SERVER_USER=root

# the location of a file that the PID of this script should be stored in. This PID will be written into a text file named etpro_pid in this case.
PIDFILE=/tmp/etpro_pid;

#
# You Do not have to change anything below here
# 
#

etded_start() {
  echo -n "Starting Server:"
  if [ -f $SCRIPT_PIDFILE ]; then
  PID='cat $SCRIPT_PIDFILE'
  echo Server already running: $PID
  exit 2;
  elif [ -f $SERVER_PIDFILE ]; then
  PID='cat $SERVER_PIDFILE'
  echo Server already running: $PID
  exit 2;
  else
echo $$ > $PIDFILE;
while [ 1 ]; do
	if [ ! $(pidof $SERVER_BIN) ]; then 
			# screen -d -m su - \
			# '$SERVER_BIN' $SERVER_OPTIONS 
			$SERVER_BIN $SERVER_OPTIONS
			sleep 10
		if [ ! $(pidof $SERVER_BIN) ]; then
			echo "\
Could not find a PID for ${SERVER_BIN}! 
If etded.x86 is actually running, update ${0} 
so that it can properly detect the PID of ${SERVER_BIN}."
			rm $PIDFILE 
			exit;
		fi
		echo `pidof $SERVER_BIN` > $SERVER_PIDFILE
	fi
	sleep 30;
done;
  fi
  echo -n "Server has been Started."
}

etded_stop() {
  echo -n "Shutting Down Server:"
  if [ -r $SCRIPT_PIDFILE ]; then
	kill `cat $SCRIPT_PIDFILE`
	rm $SCRIPT_PIDFILE 
  fi 
  if [ -r $SERVER_PIDFILE ]; then
	kill `cat $SERVER_PIDFILE`
	rm $SERVER_PIDFILE
  else
	killall $SERVER_BIN
  fi
  echo -n "Server Shuttdown Completed"
}

etded_restart() {
  echo -n "Restarting Server:"
  etded_stop
  sleep 20
  etded_start
  echo -n "Server has been Restarted."
}

case "$1" in
'start')
  etded_start
  ;;
'stop')
  etded_stop
  ;;
'restart')
  etded_restart
  ;;
*)
  echo "usage $0 start|stop|restart"
esac

# best regards, CHESTER. www.groundzeroservers.com
Some may think they already have this script, but they don't. There were errors in the script I was given, so I went and made my own.

Hope this helps everyone.
Chester
Live Jaymod Help: click here (java)
Image

User avatar
Snipey
Moderator
Posts: 1094
Joined: Sun Jul 24, 2005 9:17 pm
Location: Illinois, USA
Contact:

Post by Snipey » Fri Jun 16, 2006 11:26 am

stickied.
ImageImage
Image
[JOT$]Homepage

There are only 10 kinds of people in the world; those who understand binary, and those who don't.

User avatar
Chester
Jaymod Developer
Posts: 985
Joined: Sun Apr 24, 2005 7:52 pm
Location: Above the 49th Parallel.
Contact:

Post by Chester » Fri Jun 16, 2006 12:30 pm

I posted this because most of the time, when someone is setting up a server on linux, they sometimes want to run multiple instances of a program and be able to easily control them (especially on a dedicated server). I've been given a few scripts that did not do what I wanted them to, or did it in a very strange (deranged) way that I thought may have been just from lack of understanding.

So, I spent a couple days researching linux basic, intermediate and advanced scripting and came up with this script, tested it, and then posted it here for everyone to use. Now I know most people are unable to learn, or want to learn, advanced scripting for linux in a short period of time so I am saving you the trouble with this script. Plus the fact that it is somewhat painfull, or annoying, to go looking for a script to do just this.

There are many ways to write something like this in linux, so expect comments about it or possible other suggestions...

...I should add a status feature...
Live Jaymod Help: click here (java)
Image

deej
Server n00b
Posts: 36
Joined: Tue Sep 06, 2005 11:04 am
Location: Belgium
Contact:

Post by deej » Sun Jul 23, 2006 4:20 am

Very nice script but the While [ 1 ] loop in etded_start does confuse me. Having this in means you should run your script with the & parameter attached (e.g. ./startmyserver &) to put the script in the background, otherwise you'll never get your command prompt back. This is why tjw (etpub developer) split his script into 2: one to start the server and one to monitor the server. You can then have the monitor script run every minute with cron so that the server restarts itself automatically after 1 minute of downtime.

I do have one serious objection:

Code: Select all

SERVER_USER=root
In good Linux practice I'd say to run etded.x86 as a separate user that cannot log in. The 2.60 binary was vulnerable to a buffer overflow so if you run 2.60 (not 2.60b) as root, and someone exploits the vulnerability, you have just given root access to your system.

For those of you who want to add a non-privileged user to your system, here's an example:

We'll add a user called "gamed" (as short for game daemon) and put him in a group "gamed". The userID and groupID are not that important, but in this example I'll use the group number 500. For the user's home directory we'll use the path where your ET server resides (I use /srv/et as an example).

So start by adding a group in which the user has to go. You can put the user in an existing group but let's be paranoid, we don't want that cause you might put it in the "root" group ;):

Code: Select all

groupadd -g 500 gamed
We now have a group gamed. Verify this by typing cat /etc/group. You should see something like this:

Code: Select all

root:x:0:root
bin:x:1:bin,daemon
daemon:x:2:bin,daemon
sys:x:3:bin,adm
...
gamed:x:500:
Now let's add the user:

Code: Select all

useradd --gid 500 -d /srv/et -s /sbin/nologin gamed
This tells the system to create user gamed, put him in group gamed (500), set his home dir to /srv/et and tells the system that gamed cannot login.

Verify this by doing: cat /etc/passwd. You should see something like this:

Code: Select all

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
...
gamed:x:500:500::/srv/et:/sbin/nologin
Looking good but you're not done yet. We need to modify some permissions & ownerships on the /srv/et folder before we start. We'll set the owner of the files to gamed and make them writeable for gamed only (root can always write too, don't worry).

Code: Select all

chown -R gamed:gamed /srv/et
This makes all files in /srv/et owned by gamed and put in the group gamed.

Code: Select all

chmod -R 744 /srv/et
This makes all files in /srv/et executable but only by gamed. Strictly speaking only etded.x86 should be executable and nothing else but this is easier. If you're really paranoid like me you can do:

Code: Select all

chmod -R 644 /srv/et
chmod +x /srv/et/etded.x86
So if you do ls -l /srv/et you should see something like this now:

Code: Select all

-rw-r--r-- 1 gamed gamed     189 Oct 28  2005 et
-rw-r--r-- 1 gamed gamed     192 Oct 28  2005 etded
-rwxr--r-- 1 gamed gamed  709216 May  8 23:02 etded.x86
drwxr-xr-x 2 gamed gamed    4096 May 28 12:49 etmain
drwxr-xr-x 4 gamed gamed    4096 May 30 20:53 jaymod
-rw-r--r-- 1 gamed gamed 1604328 May  8 23:02 et.x86
drwxr-xr-x 6 gamed gamed    4096 Jul  7 17:01 pb
You're set. If someone hacks your server, they'll end up in a non-priviliged user that can't login.
Last edited by deej on Sun Jul 30, 2006 3:34 pm, edited 1 time in total.
reT!reD 0ld Farts - fun clan

Image

User avatar
[FL]Blindman
Server Pro
Posts: 864
Joined: Thu Jun 02, 2005 3:07 pm
Location: Champlain Valley

Post by [FL]Blindman » Tue Jul 25, 2006 4:28 pm

Im a bit curious here , why the use of screen? Is it because a lot of server hosts don't give real shell access, but something more along the lines of webmin or usermin?
I'm reasonably sure it would work without it, but thats me.
Image
Image

deej
Server n00b
Posts: 36
Joined: Tue Sep 06, 2005 11:04 am
Location: Belgium
Contact:

Post by deej » Tue Jul 25, 2006 4:37 pm

[FL]Blindman wrote:Im a bit curious here , why the use of screen? Is it because a lot of server hosts don't give real shell access, but something more along the lines of webmin or usermin?
I'm reasonably sure it would work without it, but thats me.
Screen is just a way of making the server run in - well - another screen.

It means you can login, reattach the screen where the server is running in and sit directly in the console. When done, detach the screen & log out.

This of course is only for people with shell access.
reT!reD 0ld Farts - fun clan

Image

User avatar
jaybird
Jaymod Developer
Posts: 2733
Joined: Thu Feb 24, 2005 12:27 pm
Location: San Antonio, TX
Contact:

Post by jaybird » Tue Jul 25, 2006 4:55 pm

Welcome to the boards deej ;]

deej
Server n00b
Posts: 36
Joined: Tue Sep 06, 2005 11:04 am
Location: Belgium
Contact:

Post by deej » Tue Jul 25, 2006 5:03 pm

jaybird wrote:Welcome to the boards deej ;]
Thanks :)!
reT!reD 0ld Farts - fun clan

Image

User avatar
Zeus
Moderator
Posts: 1209
Joined: Wed May 18, 2005 3:29 pm
Location: Buffalo NY
Contact:

Post by Zeus » Tue Jul 25, 2006 5:47 pm

As Deej said, without the screen, when you close the window, the process stops, closing with screen allows it to continue.

User avatar
[FL]Blindman
Server Pro
Posts: 864
Joined: Thu Jun 02, 2005 3:07 pm
Location: Champlain Valley

Post by [FL]Blindman » Tue Jul 25, 2006 7:19 pm

Gorrdy wrote:As Deej said, without the screen, when you close the window, the process stops, closing with screen allows it to continue.
Um, no it doesnt' at least not for me. I know it does for some.

Code: Select all

cd "/home/public/"
sleep 2
killall etded1.x86
sleep 2 
./etded1 +set fs_homepath /home/public +set net_ip xxxxxxxx +set com_hunkmegs xxx +set fs_game jaymod +set net_port 27967 +exec server.cfg $   &
exit $? 
Rather simplistic, but I can close the window without losing the server. the '&' is on the same line. Of course I'm doing this on a lan, not really sure if that makes a difference.

etded, or etded1 in my case---

Code: Select all

#!/bin/sh
# Needed to make symlinks/shortcuts work.
# the binaries must run with correct working directory
cd "/home/public/"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
exec ./etded1.x86 "$@"
I did try to make this work on webmin, in that case it does kill the server when you close the browser window, which is why I asked about screen.
OS is rhel3 if that matters.

/edit I did play with this script a bit, it seems it doesnt automatically create a pid file no matter how hard I try, though the process id shows up in a ps-e command.
I can hear the groans now, I just have no reason to start et through init.d. That being said, Linux noob that I am, neither version of that script seems to work right, my problem with it is it just doesnt create the pid.
Correct me if I'm wrong, but won't creating the user, then chown'ing and chgrp'ing the files accomplish the same thing?
Image
Image

User avatar
Chester
Jaymod Developer
Posts: 985
Joined: Sun Apr 24, 2005 7:52 pm
Location: Above the 49th Parallel.
Contact:

Post by Chester » Wed Jul 26, 2006 9:36 am

When I attempted to use tjw script, following directions explicitly, I had to manually kill the process when I wanted to stop the server. The restart and stop option would appear to work but actually didn't do anything for me. So I tried to make this one, tested it, and found it worked fine on my test machine and on my server.

And yeah, using screen makes it so that you can detach and retach it later to do what you want with it. On my previous linux machine that script from tjw worked fine, but on this new machine it just turns into a process that can only be killed manually and disabling the user running it. I just tried to setup this script so that it would run as a regular process that is restarted/stopped/started using P&IDs.
Live Jaymod Help: click here (java)
Image

User avatar
Chester
Jaymod Developer
Posts: 985
Joined: Sun Apr 24, 2005 7:52 pm
Location: Above the 49th Parallel.
Contact:

Post by Chester » Wed Jul 26, 2006 9:37 am

And, welcome to the boards :)

It's good to get feedback on such things.
Live Jaymod Help: click here (java)
Image

User avatar
[FL]Blindman
Server Pro
Posts: 864
Joined: Thu Jun 02, 2005 3:07 pm
Location: Champlain Valley

Post by [FL]Blindman » Wed Jul 26, 2006 9:51 am

I was trying to find out if it was related to the particular distro, I've never been able to use init to start/stop et, which is why I came up with what I did.
And_sorry_for_interrupting.
Image
Image

User avatar
jaybird
Jaymod Developer
Posts: 2733
Joined: Thu Feb 24, 2005 12:27 pm
Location: San Antonio, TX
Contact:

Post by jaybird » Wed Jul 26, 2006 10:38 am

tjw's scripts did work correctly if you got it just right. MrMxyzptlk just came up with a pretty badass scripts yesterday that saved my ass for sure.

User avatar
[FL]Blindman
Server Pro
Posts: 864
Joined: Thu Jun 02, 2005 3:07 pm
Location: Champlain Valley

Post by [FL]Blindman » Wed Jul 26, 2006 10:59 am

Jay, could you define 'just right'? I've been messing with this off and on for about a year, never got it to work right through init. It will work just fine with shell access, but thats it, webmin either breaks it or theres something im not doing.
Image
Image

Post Reply