games-server/fbmuck
[portage-squeep] / games-server / fbmuck / files / fbmuckctl
1 #!/bin/bash
2 # fbmuckctl This script takes care of starting and stopping
3 # FuzzBall Muck servers on this machine.
4 #
5 # The format of /etc/fbmucks is:
6 #
7 # MUCKNAME USERNAME MUCK_ROOT_PATH SCRIPTNAME PORTS
8 # tygmuck3 tygryss /home/revar/tygmuck restart 8888,8899s
9 # feepmuck foxen /home/foxen/muck restart 8800
10 #
11 # Port numbers are separate by commas. An 's' at the end of a port number
12 # means that that port is designated as a secure SSL port.
13
14 [ -f /etc/fbmucks ] || exit 0
15
16 RETVAL=0
17 pidfile="netmuck.pid"
18 who=`whoami`
19 cmd=$1
20 shift
21 mucknames=$@
22
23 # See how we were called.
24 case "$cmd" in
25 start)
26 # Start mucks.
27 cat /etc/fbmucks | \
28 grep -v '^[ ]*#' |\
29 grep -v '^[ ]*$' |\
30 while read name user path script ports; do
31 if [ "x$mucknames" != "x" ]; then
32 found=0
33 for muckname in $mucknames; do
34 if [ "x$muckname" = "x$name" ]; then
35 found=1
36 fi
37 done
38 if [ $found -eq 0 ]; then
39 continue
40 fi
41 fi
42 if [ "x$who" = "xroot" -o "x$who" = "x$user" ]; then
43 ports=`echo $ports | sed 's/,/ /g' | sed 's/\([0-9]*\)s/-sport \1/g'`
44 rm -f $path/$pidfile
45 echo -n "Starting $name fbmuck: "
46 failed=0
47 if [ "x$who" = "x$user" ]; then
48 $path/$script $ports
49 if [ $? == 0 ]; then
50 echo "started $name fbmuck"
51 else
52 failed=1
53 echo "failed to start $name fbmuck"
54 fi
55 else
56 rcode=`su $user -c "$path/$script $ports; echo \\\$?"`
57 if [ "$rcode" == 0 ]; then
58 echo "started $name fbmuck"
59 else
60 failed=1
61 echo "failed to start $name fbmuck"
62 fi
63 fi
64 if [ $failed == 0 ]; then
65 while [ ! -f $path/$pidfile ]; do
66 echo "DEBUG: waiting for $path/$pidfile to exist ..."
67 sleep 1
68 done
69 fi
70 echo ""
71 fi
72 done
73 ;;
74 stop)
75 # Stop mucks.
76 cat /etc/fbmucks | \
77 grep -v '^[ ]*#' |\
78 grep -v '^[ ]*$' |\
79 while read name user path script ports; do
80 if [ "x$mucknames" != "x" ]; then
81 found=0
82 for muckname in $mucknames; do
83 if [ "x$muckname" = "x$name" ]; then
84 found=1
85 fi
86 done
87 if [ $found = 0 ]; then
88 continue
89 fi
90 fi
91 if [ "x$who" = "xroot" -o "x$who" = "x$user" ]; then
92 didfail=1
93 echo -n "Shutting down $name fbmuck: "
94 if [ -f $path/$pidfile ]; then
95 pid=`cat $path/$pidfile`
96 if [ -d /proc/$pid ]; then
97 if kill $pid; then
98 didfail=0
99 else
100 didfail=1
101 fi
102 # Wait for server to complete a clean shutdown.
103 # If the process doesn't change status for a period
104 # longer than sixty seconds, assume it is hung, and exit.
105 laststat="S"
106 limitcnt=60
107 while [ -d /proc/$pid ]; do
108 newstat=`grep 'State:' /proc/$pid/status|awk '{print $2}'`
109 if [ "x$newstat" != "xR" -a "x$laststat" = "x$newstat" ]; then
110 limitcnt=`expr $limitcnt - 1`
111 if [ $limitcnt -eq 0 ]; then
112 didfail=1
113 break
114 fi
115 else
116 limitcnt=60
117 laststat=$newstat
118 fi
119 sleep 1
120 done
121 fi
122 rm -f $path/$pidfile
123 fi
124 [ $didfail = 0 ] \
125 && echo "stopped $name fbmuck" \
126 || echo "failed to stop $name fbmuck"
127 echo ""
128 fi
129 done
130 ;;
131 reload)
132 $0 stop
133 $0 start
134 ;;
135 restart)
136 $0 stop
137 $0 start
138 ;;
139 status)
140 cat /etc/fbmucks | \
141 grep -v '^[ ]*#' |\
142 grep -v '^[ ]*$' |\
143 while read name user path script ports; do
144 if [ "x$mucknames" != "x" ]; then
145 found=0
146 for muckname in $mucknames; do
147 if [ "x$muckname" = "x$name" ]; then
148 found=1
149 fi
150 done
151 if [ $found -eq 0 ]; then
152 continue
153 fi
154 fi
155 if [ "x$who" = "xroot" -o "x$who" = "x$user" ]; then
156 echo -n "fbmuck for $name "
157 if [ -f $path/$pidfile ]; then
158 pid=`cat $path/$pidfile`
159 if [ -d /proc/$pid ]; then
160 echo "is running. ($pid)"
161 else
162 echo "is not running."
163 fi
164 else
165 echo "is not running."
166 fi
167 fi
168 done
169 ;;
170 *)
171 echo -n "Usage: $0 {start|stop|restart|status}"
172 if [ "x$who" = "xroot" ]; then
173 echo " [muckname,[muckname,[..]]]"
174 else
175 echo ""
176 fi
177 exit 1
178 esac
179
180 exit 0
181