在Ubuntu 14.04上,我想通过upstart控制Java进程。如果停止,则应通过upstart来启动。
如下所示,通过start-saasservice.sh
和stop-saasservice.sh
控制启动和停止Java进程
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."
阅读了新贵的文档后,我可以想到以两种方式编写saasservice.conf
。
方法-1-从script
调用start-saasservice.sh,如果启动终止,则让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
我是新手。有人可以让我知道推荐的方法吗?