Wednesday, April 13, 2011

Geoserver Daemon

Now I am going to setup my geoserver installation to run on system startup as a daemon. This is quite nice so you don't have to manually run the geoserver process every time you log into your server. This is quite easy to do also, but you have to get a few things right or it will not work and it can be difficult to understand why.

First go ahead and download a little script file from the Geoserver website. You can use the handy little wget utility to download the file straight from the web to your choice of directory...
$ cd /full/path/to/geoserver/
$ sudo wget -O
$ sudo ln -s /full/path/to/geoserver/ /etc/init.d/geoserver
$ sudo chmod +x
$ sudo flip -ub
Notice that we named it and then created a link file to it at /etc/init.d/geoserver. You could just copy the script directly to this path, renaming the file as geoserver, but typically the script is kept with the application binaries and a link is used to reference it from this location. We also run flip on this file to make sure Windows hasn't messed up the carriage returns and line feeds.

Now we need to modify some of the settings in this file so open up the /etc/init.d/geoserver file using your favorite text editor. Below is the entire script file with the important lines being 16, 17, 22, 23. These are the settings that I had to change to fit my server configuration. Notice that by default this file is setup to run if the user account you are working under is named "geoserver". Also, notice that I added the " -server" string to the end of the JAVA_OPTS variable on line 23. Honestly, I have no idea what it does, but many resources I read said to do that :) Thank you very much to Lennart for writing this script!

#! /bin/sh
# Provides:          geoserver
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: GeoServer OGC server

# Author: Lennart Jütte <>

# Geoserver configuration - use /etc/default/geoserver to override these vars
# user that shall run GeoServer

DESC="GeoServer daemon"
JAVA_OPTS="-Xms128m -Xmx512m -server"

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME


# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions


 # Return
 #   0 if daemon has been started
 #   1 if daemon was already running
 #   2 if daemon could not be started

 start-stop-daemon --start --pidfile $PIDFILE --make-pidfile \
  --chuid $USER --chdir $GEOSERVER_HOME \
  -b --test --exec $DAEMON -- $DAEMON_ARGS > /dev/null \
  || return 1

 start-stop-daemon --start --pidfile $PIDFILE --make-pidfile \
  --chuid $USER --chdir $GEOSERVER_HOME \
  -b --exec $DAEMON -- $DAEMON_ARGS \
  || return 2


 # Return
 #   0 if daemon has been stopped
 #   1 if daemon was already stopped
 #   2 if daemon could not be stopped
 #   other if a failure occurred

 start-stop-daemon --stop --pidfile  $PIDFILE \
  --user $USER \

 [ "$RETVAL" = 2 ] && return 2

 # Many daemons don't delete their pidfiles when they exit.
 rm -f $PIDFILE
 return "$RETVAL"


case "$1" in
 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
 case "$?" in
  0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
 case "$?" in
  0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
 log_daemon_msg "Restarting $DESC" "$NAME"
 case "$?" in
  case "$?" in
   0) log_end_msg 0 ;;
   1) log_end_msg 1 ;; # Old process is still running
   *) log_end_msg 1 ;; # Failed to start
    # Failed to stop
  log_end_msg 1
 #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
 echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
 exit 3



Again make sure to flip this file to make it readable on linux and double or triple check your paths and other options.

Next you will need to set the geoserver to launch on startup. I don't understand the details of this technology since I have relatively new to linux, but it is something akin to windows services in the Microsoft world. Regardless, just run the following command and it will setup Geoserver to run as a service when the system starts up...
$ sudo update-rc.d geoserver defaults
You can put a number at the end to set the start order if you have a problem. Adding it to the end is probably a good idea like so...
$ sudo update-rc.d geoserver defaults 99
After doing this you should be able to start Geoserver by starting the daemon...
$ sudo /etc/init.d/geoserver start
Also, you can remove this service using the following command...
$ sudo update-rc.d -f geoserver remove
Make sure that this command them returns something that looks like a geoserver process running as a java application...
$ ps aux | grep geoserver
Now try logging into the administrative website again using a browser pointed to...
If not it could be a million things that went wrong. Check your paths, permission, user, etc. and try again. It took me about three attempts before I stopped making stupid mistakes.

Of course this is not a secure installation so again we need to read the following resources to tighten it all down.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.