wiki:UbuntuRunningDeepaMehtaAsDaemon
Last modified 8 years ago Last modified on 08.12.2016 12:22:42

Running DeepaMehta as a Daemon on Ubuntu 12.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.

There is an installation script provided by JuergeN which you can clone from https://github.com/digitalmemex/dm4-debianized-install. This is the easiest installation for all Debian/Ubuntu users. If you are looking for a manual installation instruction, just follow this manual.

Download DeepaMehta

Download the latest stable version of DeepaMehta from our build server:

wget http://demo.deepamehta.de/download/deepamehta-4.XXX.YYY.zip

Unzip zip archive

unzip deepamehta-4.XXX.YYY.zip

Remove Felix Gogo Shell

As we are running in daemon mode, we cannot use the Felix Gogo Shell and need to remove it (3 files):

rm ./bundles/org.apache.felix.gogo.*

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

the lazy way

Copy the DeepaMehta files to /opt/deepamehta:

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

the Debian way

DIRECTORY			USER			ACL	COMMENT
===========================================================================================
/etc/deepamehta			root:deepamehta		750	# config files
/usr/share/deepamehta		root:deepamehta		750	# bin dir, bundle dir, bundle-deploy
/var/lib/deepamehta		deepamehta:deepamehta	750	# db dir, file dir
/var/cache/deepamehta		deepamehta:deepamehta	750	# bundle-cache dir	
/var/run/deepamehta		deepamehta:deepamehta   750	# pid files
/var/log/deepamehta		deepamehta:deepamehta	755	# logfiles
/usr/share/doc/deepamehta	root:root		755	# readme, license, examples

Create start stop script

Create a script 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

# NAME is the name of the instance of deepamehta
NAME="deepamehta"
#NAME=$( echo $0 | awk -F'/' '{ print $NF }' )
DESC="DeepaMehta daemon"
USER="deepamehta"
DAEMON=/usr/share/deepamehta/deepamehta-daemon
EXTRA_ARGS="--chuid $USER:$USER --background --make-pidfile"
DAEMON_ARGS="/etc/deepamehta/$NAME.conf"
PIDFILE=/var/run/$USER/$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

# create PID directory
if [ ! -d /var/run/$USER ]; then
    mkdir /var/run/$USER
    chown $USER:$USER /var/run/$USER
    sleep 2
fi

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

# Create logfile if configured
if [ ! -z $LOG_DEEPAMEHTA_INIT ] && [ ! -f $LOG_DEEPAMEHTA_INIT ]; then
    touch $LOG_DEEPAMEHTA_INIT
    chown $USER:$USER $LOG_DEEPAMEHTA_INIT
fi

# 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 $USER -u $USER o pid= o args= | sed s'/^\ //' | grep $DAEMON_ARGS | awk -F' ' '{ print $1 }' )
    if [ "$PIDLNUM" != "" ]; then
	if [ -f $PIDFILE ]; then
	    PIDFNUM=$( cat $PIDFILE )
	    if [ "$PIDLNUM" != "$PIDFNUM" ]; then
		echo "   Error in pidfile $PIDFILE! - Is there another $NAME process running for user $USER?"
		return 2
	    else
		return 0
	    fi
	else
	    echo "   WARNING! Found running instance $NAME of $DESC with PID $PIDLNUM and user $USER, but no valid pidfile $PIDFILE was found."
	    return 2
	fi
    else
	return 1
    fi
}

#
# Function that starts the daemon/service
#
do_start()
{
	# Check START_DEEPAMEHTA
	if [ "$START_DEEPAMEHTA" != "yes" ]; then
	    echo "   $DESC is disabled in /etc/default/$NAME."
	    exit 0
	fi
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	echo -n "   Starting instance $NAME of $DESC ... "
	status_of_daemon || ( [ "$?" = 1 ] && start-stop-daemon --start $EXTRA_ARGS --quiet --pidfile $PIDFILE --exec $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 instance $NAME of $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)
	status_of_daemon || ( [ "$?" = 1 ] && ( [ ! -z $LOG_DEEPAMEHTA_INIT ] && echo "$( date +%d.%m.%Y" "%H:%M:%S ) *** Starting $DESC ***">>$LOG_DEEPAMEHTA_INIT ); do_start )
	;;
  stop)
	status_of_daemon && ( do_stop; ( [ ! -z $LOG_DEEPAMEHTA_INIT ] && echo "$( date +%d.%m.%Y" "%H:%M:%S ) *** Stopping $DESC ***">>$LOG_DEEPAMEHTA_INIT ) )
	;;
  status)
       status_of_daemon && echo "   Instance $NAME of $DESC is running with PID $PIDLNUM." || echo "   Instance $NAME of $DESC is not running."
       ;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	echo " Restarting instance $NAME of $DESC"
	status_of_daemon && do_stop
	case "$?" in
	  0|1)
		( [ ! -z $LOG_DEEPAMEHTA_INIT ] && echo "$( date +%d.%m.%Y" "%H:%M:%S ) *** Restarting $DESC ***">>$LOG_DEEPAMEHTA_INIT ); do_start
		;;
	  *)
	  	# 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

That's it. :)


Continue with Configuring Apache2 Server as SSL Proxy with htdigest auth or Securing DeepaMehta
Back to Documentation