Auto start your service application on Linux boot

Thanks /etc/init.d/cherokee.

1, cd /etc/init.d/, add a script file named your-custom-server with contents as follows:

#! /bin/sh
#
# start/stop your-custom-server
### replace all your-custom-servers and delete this line

### BEGIN INIT INFO
# Provides:          your-custom-server
# Required-Start:    $remote_fs $network $syslog
# Required-Stop:     $remote_fs $network $syslog
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start your-custom-server
# Description:       Start your-custom-server
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/your-custom-server
NAME=your-custom-server
PIDFILE=/var/run/your-custom-server.pid

. /lib/lsb/init-functions

set -e

test -x $DAEMON || exit 0

case "$1" in
  start)	
	printf "Starting your-custom-server: %s\t" "$NAME"
	start-stop-daemon --start --oknodo --pidfile $PIDFILE --exec $DAEMON -b
	;;

  stop)
	printf "Stopping your-custom-server: %s\t" "$NAME"
	start-stop-daemon --stop --oknodo --pidfile $PIDFILE --exec $DAEMON
	rm -f $PIDFILE
	;;

  restart)
	$0 stop
	sleep 1
	$0 start
	;;

  reload|force-reload)
	printf "Reloading your-custom-server: %s\t" "$NAME"
	if [ -f $PIDFILE ]
          then
	    PID=$(cat $PIDFILE)
	    if ps p $PID | grep $NAME >/dev/null 2>&1
                then
		kill -HUP $PID
	    else
		echo "PID present, but $NAME not found at PID $PID - Cannot reload"
		exit 1
	    fi
	else
	    echo "No PID file present for $NAME - Cannot reload"
	    exit 1
	fi
	;;

  status)
	# Strictly, LSB mandates us to return indicating the different statuses,
	# but that's not exactly Debian compatible - For further information:
	# http://www.freestandards.org/spec/refspecs/LSB_1.3.0/gLSB/gLSB/iniscrptact.html
	# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=208010
	# ...So we just inform to the invoker and return success.
	printf "%s status:\t" "$NAME"
	if [ -e $PIDFILE ]; then
	    PROCNAME=$(ps -p $(cat $PIDFILE) -o comm=)
	    if [ "x$PROCNAME" = "x" ]; then
		    printf "Not running, but PID file present \t"
	    else
		    if [ "$PROCNAME" = "$NAME" ]; then
		        printf "Running\t"
		    else
		        printf "PID file points to process '%s', not '%s'\t" "$PROCNAME" "$NAME"
		    fi
	    fi
	else
	    if PID=$(pidofproc $DAEMON); then
		    printf "Running (PID %s), but PIDFILE not present\t" "$PID"
	    else
		    printf "Not running\t"
	    fi
	fi
	;;

  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
	exit 1
	;;
esac

if [ $? = 0 ]; then
    echo .
    exit 0
else
    echo failed
    exit 1
fi

exit 0

2, make the script file excutable

sudo chmod +x your-custom-server

3, apply the changes

sudo update-rc.d your-custom-server defaults

4, just reboot your box, and you will see something happened

sudo reboot
posted @ 2011-06-21 12:02  ALLI Look for Lost Idylls  阅读(1145)  评论(0编辑  收藏  举报