Skip to content

Instantly share code, notes, and snippets.

@imesh
Last active August 2, 2016 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save imesh/5256272cd71b74a06581 to your computer and use it in GitHub Desktop.
Save imesh/5256272cd71b74a06581 to your computer and use it in GitHub Desktop.
An init.d script for Apache Stratos
#!/bin/sh
# ------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# ------------------------------------------------------------------
# Description: Use this script for creating an init.d service for Stratos.
# Author: imesh@apache.org
# ------------------------------------------------------------------
### BEGIN INIT INFO
# Provides: stratos
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Start/stop stratos server
### END INIT INFO
USER="vagrant"
PRODUCT_NAME="stratos"
JAVA_HOME="/opt/jdk1.7.0_60"
PRODUCT_HOME="/opt/apache_stratos_4.1.0_SNAPSHOT"
PID_FILE="${PRODUCT_HOME}/wso2carbon.pid"
CMD="${PRODUCT_HOME}/bin/stratos.sh"
# LSB exit codes:
# ftp://ftp.nomadlinux.com/nomad-2/dist/heartbeat-1.2.5/include/clplumbing/lsb_exitcodes.h
LSB_EXIT_OK=0
LSB_EXIT_GENERIC=1
LSB_EXIT_EINVAL=2
LSB_EXIT_ENOTSUPPORTED=3
LSB_EXIT_EPERM=4
LSB_EXIT_NOTINSTALLED=5
LSB_EXIT_NOTCONFIGED=6
LSB_EXIT_NOTRUNNING=7
is_service_running() {
if [ -e ${PID_FILE} ]; then
PID=`cat ${PID_FILE}`
if ps -p $PID >&- ; then
# service is running
return 0
else
# service is stopped
return 1
fi
else
# pid file was not found, may be server was not started before
return 1
fi
}
# Status the service
status() {
is_service_running
service_status=$?
if [ "${service_status}" -eq 0 ]; then
echo "${PRODUCT_NAME} service is running"
return ${LSB_EXIT_OK}
elif [ "${service_status}" -eq 1 ]; then
echo "$PRODUCT_NAME service is stopped"
return ${LSB_EXIT_OK}
else
echo "$PRODUCT_NAME service status is unknown"
return ${LSB_EXIT_GENERIC}
fi
}
# Start the service
start() {
if is_service_running; then
echo "${PRODUCT_NAME} service is already running"
return ${LSB_EXIT_OK}
fi
echo "starting ${PRODUCT_NAME} service..."
su - ${USER} -c "export JAVA_HOME=${JAVA_HOME}; ${CMD} start"
is_service_running
service_status=$?
while [ "$service_status" -ne "0" ]
do
sleep 1;
is_service_running
service_status=$?
done
echo "${PRODUCT_NAME} service started"
return ${LSB_EXIT_OK}
}
# Restart the service
restart() {
echo "restarting ${PRODUCT_NAME} service..."
su - ${USER} -c "export JAVA_HOME=${JAVA_HOME}; ${CMD} restart"
echo "${PRODUCT_NAME} service restarted"
return ${LSB_EXIT_OK}
}
# Stop the service
stop() {
if ! is_service_running; then
echo "${PRODUCT_NAME} service is already stopped"
return ${LSB_EXIT_OK}
fi
echo "stopping ${PRODUCT_NAME} service..."
su - ${USER} -c "export JAVA_HOME=${JAVA_HOME}; ${CMD} stop"
is_service_running
service_status=$?
while [ "$service_status" -eq "0" ]
do
sleep 1;
is_service_running
service_status=$?
done
echo "${PRODUCT_NAME} service stopped"
return ${LSB_EXIT_OK}
}
### main logic ###
case "$1" in
start)
start
;;
stop|graceful-stop)
stop
;;
status)
status
;;
restart|reload|force-reload)
restart
;;
*)
echo $"usage: $0 {start|stop|graceful-stop|restart|reload|force-reload|status}"
exit 1
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment