File: initscript

package info (click to toggle)
lambdamoo 1.8.1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,020 kB
  • ctags: 2,785
  • sloc: ansic: 25,602; yacc: 1,057; makefile: 389; sh: 176; csh: 20
file content (149 lines) | stat: -rw-r--r-- 3,909 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/sh
#
# initscript for lambdamoo, by Jonathan Walther <krooger@debian.org>
#
# Edit /etc/moos.conf to change lambdamoo's configuration.

test -f /etc/lambdamoo.conf || exit 0

# xxx Read config file.
# xxx This is WRONG
# xxx in the next version, will have to parse this file and do some
# xxx other funky stuff.
. /etc/lambdamoo.conf

# Sanity check on config file.
if [ "$PORT" = "" ] || [ "$LOGFILE" = "" ] || [ "$TIMEOUT" = "" ]; then
	echo "LambdaMoo: bad /etc/lambdamoo.conf !" ;
	exit
fi

# See how we were called.
case "$1" in
  start)
        # Check what the file /var/lib/lambdamoo/moo.db points to.
        DBFILE=`update-alternatives --display moo.db | \
                grep "currently points to" | \
                cut -d " " -f 6`
        test -f "$DBFILE" || exit 0

	# Clean up old databases.
	cd `dirname $DBFILE`
	if [ ! -r "$DBFILE" ] ; then
		echo "Bad database: $DBFILE"
		exit 1
	fi
	if [ -r "$DBFILE.new" ] ; then
		mv $DBFILE $DBFILE.old
		mv $DBFILE.new $DBFILE
		rm -f $DBFILE.old.gz
		gzip $DBFILE.old &
	fi

	echo -n "Starting lambdamoo servers: "

	# Important to do this so we don't get busy mount points or other
	# problems.
	cd /

	# Check if there is a process to match what's in the pid file,
        # by sending signal 0, which has no effect. This also checks to see
        # if there is a pid file at all, btw.
        if start-stop-daemon --quiet --stop --signal 0 --user daemon \
		--pidfile /var/run/lambdamoo.pid --name lambdamoo 2>/dev/null
	then
		echo " already running."
		exit
	fi

	touch $LOGFILE
	chown daemon.daemon $LOGFILE

	su daemon -c "\
		/sbin/start-stop-daemon --start --quiet --user daemon \
			--exec /usr/sbin/lambdamoo \
			--startas /usr/sbin/lambdamoo -- \
			-l $LOGFILE $DBFILE $DBFILE.new $PORT" \
			>/dev/null 2>/dev/null </dev/null &
	echo $! > /var/run/lambdamoo.pid
	echo "$DBFILE" > /var/run/lambdamoo.db
	echo "."
	;;
  stop)
	echo -n "Stopping LambdaMoo servers: "
	# Check if there is a process to match what's in the pid file,
	# by sending signal 0, which has no effect. This also checks to see
	# if there is a pid file at all, btw.
	if start-stop-daemon --quiet --stop --signal 0 --user daemon \
		--pidfile /var/run/lambdamoo.pid --name lambdamoo 2>/dev/null
	then
		# Load up the filename of the db file that was last used.
		DBFILE=`cat /var/run/lambdamoo.db` 2>/dev/null

		# Signal 2 means dump db and die.
		start-stop-daemon --quiet --stop --signal 2 \
			--user daemon --name lambdamoo \
			--pidfile /var/run/lambdamoo.pid 

		# Wait until the timeout for the server to die.
		count=$TIMEOUT
		newfile=no
		fivecount=0
		pid=`cat /var/run/lambdamoo.pid`
		while ([ $count != 0 ]) do
			let count=$count-1
			let fivecount=fivecount+1
			if kill -0 "$pid" 2>/dev/null ; then
				sleep 1
				# Every five seconds, check so see if it
				# has started saving db, if not, resend the
				# signal 2.
				if [ $fivecount = 5 -a "$newfile" != "yes" ] 
				then 
					fivecount=0
					if [ -z "`ls $DBFILE.new.\#*\# 2>/dev/null`" ]
					then
						start-stop-daemon --quiet --stop --signal 2 \
							--user daemon\
							--pidfile /var/run/lambdamoo.pid \
							--name lambdamoo 2>/dev/null
						echo -n retry
						let count=$count-4
					else
						newfile=yes
					fi
				fi
			else
				count=0
			fi
			echo -n .
		done

		# If it's not dead yet, kill it.
		if  kill -0 "$pid" 2>/dev/null ; then
			echo " TIMEOUT!"
			start-stop-daemon --quiet --stop \
				-exec /usr/sbin/lambdamoo --user daemon \
				--pidfile /var/run/lambdamoo.pid \
				--name lambdamoo 2>/dev/null
		else
			echo "done."
		fi
	else
		echo " not running.";
	fi

	rm -f /var/run/lambdamoo.pid /var/run/lambdamoo.db

	;;
  force-reload|restart)
	# There doesn't appear to be a better way (like a signal we could send.)
	$0 stop
	$0 start
	;;
  *)
	echo "Usage: /etc/init.d/lambdamoo {start|stop|restart|force-reload}"
	exit 1
esac

exit 0