通过 upstart 启动、停止和监控 java jar 的执行

通过 upstart 启动、停止和监控 java jar 的执行

在 Ubuntu 14.04 上,我想通过 upstart 控制一个 java 进程。如果它停止了,应该通过 upstart 启动它。

java 进程的启动和停止通过start-saasservice.sh和来控制,stop-saasservice.sh如下所示

启动saas服务.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}

停止saas服务.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."

在阅读了 upstart 文档之后,我可以想到saasservice.conf用两种方式来编写。

方法 -1 - 调用 start-saasservice.shscript并让 upstart 在终止时启动该进程

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

方法 2 - 调用start-saasservice.sh预启动然后script继续检查服务是否正在运行。

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

我是 Upstart 新手。有人能告诉我推荐哪种方式吗?

相关内容