Skip to content

Instantly share code, notes, and snippets.

@ogasyo
Created September 13, 2011 10:00
Show Gist options
  • Save ogasyo/1213515 to your computer and use it in GitHub Desktop.
Save ogasyo/1213515 to your computer and use it in GitHub Desktop.
KyotoTycoon init script
cat > /etc/init.d/ktserver <<\_EOF_
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: ktserver
# Required-Start: $syslog
# Required-Stop: $syslog
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Kyoto Tycoon
# Description: Kyoto Tycoon
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/loca/sbin:/usr/local/bin
DESC="KyotoTycoon"
NAME=ktserver
DAEMON=/usr/local/bin/$NAME
PIDFILE=/var/ktserver/pid
SCRIPTNAME=/etc/init.d/$NAME
##################################
## KyotoTycoon Options
# configuration variables
basedir="/var/ktserver"
port="1979"
pidfile="$basedir/pid"
logfile="$basedir/log"
logopt="-le"
#ulogdir="$basedir/ulog"
#ulimsiz="256m"
#sid=1
#mhost="remotehost1"
#mport="1979"
#rtsfile="$basedir/rts"
dbname="$basedir/casket.kch#bnum=1000000"
timeout=10
th=1
plsv="/usr/local/libexec/ktplugservmemc.so"
plex="port=1978#opts=f"
maxcon="65535"
retval=0
# setting environment variables
LANG=C
LC_ALL=C
export LANG LC_ALL PATH
## /KyotoTycoon Options
##################################
##################################
## KyotoTycoon Functions
check_pidfile(){
if [ -f "$pidfile" ] ; then
_PID=`cat "$pidfile"`
if [ -d "/proc/${_PID}" ]; then
_PROC_NAME=`ps -p ${_PID} -o comm=`
if [ $_PROC_NAME = $NAME ]; then
printf 'Already existing process: %d\n' "$_PID"
return 1
else
printf 'PID %d is already using by other proccess: %s\n' "$_PID" "$_PROC_NAME"
printf 'Remove pidfile: %s\n' "${pidfile}"
rm $pidfile
fi
else
printf 'Remove ghost pidfile: %s\n' "${pidfile}"
rm $pidfile
fi
fi
return 0
}
# start the server
server_start(){
retval=0;
_ARGS=""
printf "Starting the server of $DESC\n"
mkdir -p "$basedir"
if [ -z "$basedir" ] || [ -z "$port" ] || [ -z "$pidfile" ] || [ -z "$dbname" ] ; then
printf 'Invalid configuration\n'
retval=1
elif ! [ -d "$basedir" ] ; then
printf 'No such directory: %s\n' "$basedir"
retval=1
else
(check_pidfile); retval=$?
if [ -n "$maxcon" ] ; then
ulimit -n "$maxcon" >/dev/null 2>&1
fi
_ARGS="$_ARGS -port $port -dmn -pid $pidfile"
if [ -n "$logopt" ] ; then
_ARGS="$_ARGS $logopt"
fi
if [ -n "$th" ] ; then
_ARGS="$_ARGS -th $th"
fi
if [ -n "$timeout" ] ; then
_ARGS="$_ARGS -tout $timeout"
fi
if [ -n "$plsv" ] ; then
_ARGS="$_ARGS -plsv $plsv"
fi
if [ -n "$plex" ] ; then
_ARGS="$_ARGS -plex $plex"
fi
if [ -n "$logfile" ] ; then
_ARGS="$_ARGS -log $logfile"
fi
if [ -n "$ulogdir" ] ; then
mkdir -p "$ulogdir"
_ARGS="$_ARGS -ulog $ulogdir"
fi
if [ -n "$ulimsiz" ] ; then
_ARGS="$_ARGS -ulim $ulimsiz"
fi
if [ -n "$sid" ] ; then
_ARGS="$_ARGS -sid $sid"
fi
if [ -n "$mhost" ] ; then
_ARGS="$_ARGS -mhost $mhost"
fi
if [ -n "$mport" ] ; then
_ARGS="$_ARGS -mport $mport"
fi
if [ -n "$rtsfile" ] ; then
_ARGS="$_ARGS -rts $rtsfile"
fi
if [ $retval -ne 1 ] ; then
printf "Executing: %s\n" "$_ARGS"
_ARGS="$_ARGS $dbname"
start-stop-daemon --start --quiet --pidfile $pidfile --exec $DAEMON -- $_ARGS
if [ "$?" -eq 0 ] ; then
printf 'Done\n'
else
printf 'The server could not started\n'
retval=1
fi
fi
fi
}
# stop the server
server_stop(){
retval=0
printf "Stopping the server of $DESC\n"
(check_pidfile); retval=$?
if [ $retval -eq 1 ] ; then
_PID=`cat "$pidfile"`
printf "Sending the terminal signal to the process: %s\n" "$_PID"
start-stop-daemon --stop --quiet --pidfile $pidfile --exec $DAEMON
c=0
while true ; do
sleep 0.1
if [ -f "$pidfile" ] ; then
c=`expr $c + 1`
if [ "$c" -ge 100 ] ; then
printf 'Hanging process: %d\n' "$_PID"
retval=1
break
fi
else
printf 'Done\n'
break
fi
done
else
printf 'No process found\n'
retval=1
fi
}
# send HUP to the server for log rotation
server_hup(){
retval=0
printf "Sending HUP signal to the server of $DESC\n"
if [ -f "$pidfile" ] ; then
_PID=`cat "$pidfile"`
printf "Sending the hangup signal to the process: %s\n" "$_PID"
kill -HUP "$_PID"
printf 'Done\n'
else
printf 'No process found\n'
retval=1
fi
}
## /KyotoTycoon Functions
##################################
case "$1" in
start)
server_start
;;
stop)
server_stop
;;
restart)
server_stop
server_start
;;
reload)
server_hup
;;
*)
echo "Usage: /etc/init.d/$NAME [start / stop / restart / reload]"
exit 1
;;
esac
exit $retval
_EOF_
chmod a+x /etc/init.d/ktserver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment