1

On Ubuntu 14.04 I want to control a java process via upstart. If it gets stopped it should be started via upstart.

The start and stop java process is controlled via the start-saasservice.sh and stop-saasservice.sh as shown below

start-saasservice.sh

#! /bin/bash
DEPLOYMENT_TYPE_FILE=deployment.info
SERVICE_PATH="/home/ubuntu/build-target/saasservice"
COMPONENT_NAME="saasservice"
LOG_ROTATOR="${SERVICE_PATH}/log-rotator.pl --dir=${LOG_PATH}/${COMPONENT_NAME} --file=${COMPONENT_NAME}.STDOUT"

nohup java \
    -XX:OnOutOfMemoryError="kill -9 %p" \
    -Ddeployment_type.path=$DEPLOYMENT_TYPE_FILE \
    -cp $SERVICE_PATH/${COMPONENT_NAME}-0.001-SNAPSHOT.jar com.vnera.SaasListener.ServiceMain \
    $SERVICE_PATH/ServiceThriftListenerConfigTemplate.properties server \
    $SERVICE_PATH/saasconfiguration.yaml \
    2>&1 | ${LOG_ROTATOR}

stop-saasservice.sh

#!/bin/bash

COMPONENT="saasservice"

echo "Stopping saasservice ..."

# Grabs and kill a process from the pidlist that has the jars name
pid=`ps aux | grep "$COMPONENT-0.001-SNAPSHOT.jar" | grep -v grep | awk {'print $2'}`
if [ ! -z "$pid" ]; then echo $pid; kill -9 $pid; fi

#Terminating log-rotator process for saasservice
lrpid=`ps aux | grep "$COMPONENT/log-rotator.pl" | grep -v grep | awk {'print $2'}`
if [ ! -z "$lrpid" ]; then
    echo "$COMPONENT/log-rotator pid - $lrpid";
    kill -9 $lrpid;
fi

echo "saasservice stopped."

After reading the upstart documentation I can think of writing the saasservice.conf in two ways.

Approach -1 - Calling the start-saasservice.sh from script and let upstart start the process if gets terminated

description "SaasListener Service"

start on (runlevel [2345] and vrni-svcinit-ntp-insync)
stop on (shutdown or vrni-svcinit-ntp-notinsync)

respawn
respawn limit unlimited

env HOME="/home/ubuntu"
env SAASSERVICE_HOME="/home/ubuntu/build-target/saasservice"

script
  echo "[`date +"%h %d %Y %H:%M:%S-%3N"`] Starting SaasListener..."
  mkdir -p $HOME/logs
  chown ubuntu $HOME/logs
  exec su -s /bin/sh -c "(cd $SAASSERVICE_HOME && $SAASSERVICE_HOME/start-saasservice.sh $SAASSERVICE_HOME || true)" ubuntu
  echo "[`date +"%h %d %Y %H:%M:%S-%3N"`] SaasListener started."
end script

post-stop script
  echo "[`date +"%h %d %Y %H:%M:%S-%3N"`] Exiting SaasListener..."
  su -s /bin/sh -c "cd $SAASSERVICE_HOME && $SAASSERVICE_HOME/stop-saasservice.sh" ubuntu
  sleep 1
  echo "[`date +"%h %d %Y %H:%M:%S-%3N"`] SaasListener exited."
end script

Approach 2 - Call start-saasservice.sh on pre-start and then on script keep checking if the service is running.

description "SAAS Service"

start on (runlevel [2345] and vrni-svcinit-ntp-insync)
stop on (shutdown or vrni-svcinit-ntp-notinsync)

respawn
respawn limit unlimited

env HOME="/home/ubuntu"
env SAASSERVICE_HOME="/home/ubuntu/build-target/saasservice"

pre-start script
  echo "[`date +"%h %d %Y %H:%M:%S-%3N"`] Starting SaasListener..."
  mkdir -p $HOME/logs
  chown ubuntu $HOME/logs
  su -s /bin/sh -c "(cd $SAASSERVICE_HOME && $SAASSERVICE_HOME/start-saasservice.sh $SAASSERVICE_HOME || true)" ubuntu
  echo "[`date +"%h %d %Y %H:%M:%S-%3N"`] SaasListener started."
end script

script
  sleep 3
  while ps -ef | grep -v "grep" | grep -q "saasservice-0.001-SNAPSHOT.jar"; do
    sleep 30
  done
  sleep 1
end script

post-stop script
  echo "[`date +"%h %d %Y %H:%M:%S-%3N"`] Exiting SaasListener..."
  su -s /bin/sh -c "cd $SAASSERVICE_HOME && $SAASSERVICE_HOME/stop-saasservice.sh" ubuntu
  sleep 1
  echo "[`date +"%h %d %Y %H:%M:%S-%3N"`] SaasListener exited."
end script

I am new to upstart. Can someone let me know which is the recommended way?

0

You must log in to answer this question.

Browse other questions tagged .