wiki:UbuntuRunningDeepaMehtaAsDaemon

Version 12 (modified by silke, 9 years ago) (diff)

--

Running DeepaMehta as a Daemon on Ubuntu 10.04 or likewise systems

You can of course run DeepaMehta on your computer by simply starting the shell script deepamehta-linux.sh in a terminal session. But maybe you want to "daemonize" the server and run it in the background. This setup is especially recommended if you want to run DeepaMehta on a server.

Download DeepaMehta

Download the latest stable version of DeepaMehta from GitHub:

wget https://github.com/downloads/jri/deepamehta/deepamehta-4.XXX.YYY.zip

Unzip zip archive

unzip deepamehta-4.XXX.YYY.zip

Create an unprivileged user

As we do not want to run DeepaMehta as root we need to create an unprivileged user:

adduser --system --home /opt/deepamehta --group --disabled-password --disabled-login --gecos DeepaMehta deepamehta

Copy files

Copy the DeepaMehta files to /opt/deepamehta:

cp -a deepamehta-4.XXX.YYY/* /opt/deepamehta
chown -R deepamehta:deepamehta /opt/deepamehta

Modify deepamehta-linux.sh

Change /opt/deepamehta/deepamehta-linux.sh to preserve PID from

#!/bin/sh
cd "$(dirname "$0")"
java -Dfile.encoding=UTF-8 -Dfelix.system.properties=file:conf/config.properties -jar bin/felix.jar

to

#!/bin/sh
cd "$(dirname "$0")"
exec java -Dfile.encoding=UTF-8 -Dfelix.system.properties=file:conf/config.properties -jar bin/felix.jar

Create start stop script

Create a scipt named deepamehta in /etc/init.d:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          deepamehta
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: DeepaMehta initscript
# Description:       This file should be used to start and stop DeepaMehta. 
#                    To be placed in /etc/init.d.
# Author:            Juergen Neumann <j.neumann{at}junes{dot}eu>
#
### END INIT INFO

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="DeepaMehta server"
NAME=deepamehta
DAEMON=/opt/deepamehta/deepamehta-linux.sh
EXTRA_ARGS="-c $NAME --user $NAME --name $NAME --background --make-pidfile"
DAEMON_ARGS=""
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
if [ ! -x "$DAEMON" ]; then 
    echo "$DAEMON not found"
    exit 0
fi

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

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

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

#
# Function that reports the status of the daemon/service
#
status_of_daemon() {
    # Return
    #   0 if daemon is running
    #   1 if daemon is not running
    #   2 on pidfile mismatch

    PIDLNUM=$( ps -U $NAME -u $NAME o pid= | sed s'/^\ //' )
    if [ "$PIDLNUM" != "" ]; then
	if [ -f $PIDFILE ]; then
	    PIDFNUM=$( cat $PIDFILE )
	    if [ "$PIDLNUM" != "$PIDFNUM" ]; then
		echo -e "\n   Error in pidfile $PIDFILE! - Is there another $NAME process running?\n"
		return 2
	    else
		return 0
	    fi
	else
	    echo -e "\n   WARNING! Found running $DESC with PID $PIDLNUM,\n   but no valid pidfile $PIDFILE was found.\n"
	    return 2
	fi
    else
	return 1
    fi
}

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	echo -n "   Starting $DESC ... "
	status_of_daemon || ( [ "$?" = 1 ] && start-stop-daemon --start $EXTRA_ARGS --quiet --pidfile $PIDFILE --startas $DAEMON $DAEMON_ARGS )
	# Add code here, if necessary, that waits for the process to be ready
	# to handle requests from services started subsequently which depend
	# on this one.  As a last resort, sleep for some time.
	
	# Sleep at least 8 seconds to check if deepamehta is running!
	sleep 8
	# Now check if we can find the according java process.
	status_of_daemon && echo "done." || ( echo "   stopped!"; [ -f $PIDFILE ] && rm $PIDFILE )
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# 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
	echo -n "   Stopping $DESC ... "
	status_of_daemon && start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE 
	RETVAL="$?"
	status_of_daemon || echo "done."
	[ "$RETVAL" = 2 ] && return 2
	# Wait for children to finish too if this is a daemon that forks
	# and if the daemon is only ever run from this initscript.
	# If the above conditions are not satisfied then add some other code
	# that waits for the process to drop all resources that could be
	# needed by services started subsequently.  A last resort is to
	# sleep for some time.
	status_of_daemon && start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --pidfile $PIDFILE
	[ "$?" = 2 ] && return 2
	# Many daemons don't delete their pidfiles when they exit.
	[ -f $PIDFILE ] && rm -f $PIDFILE
	return "$RETVAL"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	#
	# If the daemon can reload its configuration without
	# restarting (for example, when it is sent a SIGHUP),
	# then implement that here.
	#
	start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE 
	return 0
}


case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	status_of_daemon || ( [ "$?" = 1 ] && do_start )
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	status_of_daemon && do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
       status_of_daemon && echo "   $DESC is running with PID $PIDLNUM." || echo "   $DESC is not running."
       ;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	status_of_daemon && do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	# Display options
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac

:

#EOF

Change permissions:

chmod 755 /etc/init.d/deepamehta

Update rc.d

Create links for start stop runlevel:

update-rc.d deepamehta defaults

Now type /etc/init.d/deepamehta start to start DeepaMehta in the background.

Test if service is running as expected

Check if JAVA/jetty is running in the background:

ps aux | grep java | grep felix

Check (on the machine) if jetty is listening on port 8080:

nmap localhost -p 8080

should return

Starting Nmap 5.00 ( http://nmap.org ) at 2011-09-02 22:17 CEST
Interesting ports on localhost.localdomain (127.0.0.1):
PORT     STATE SERVICE
8080/tcp open  http-proxy

Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds

Drop incoming requests on port 8080:

iptables -A INPUT ! -s 127.0.0.1 -p tcp --destination-port 8080 -j REJECT

Check (from a remote client machine) if jetty is securely blocked:

nmap deepamehta.yourdomain.tld -p 8080

should return

Starting Nmap 5.00 ( http://nmap.org ) at 2011-09-02 22:17 CEST
Interesting ports on deepamehta.yourdomain.tld (xxx.xxx.xxx.xxx):
PORT     STATE    SERVICE
8080/tcp filtered http-proxy

Nmap done: 1 IP address (1 host up) scanned in 0.53 seconds

That's it. :)


Continue with Configuring Apache2 Server as SSL Proxy with htdigest auth. Back to Documentation