EDDN/systemd/start-eddn-service

121 lines
2.9 KiB
Bash
Executable File

#!/bin/bash
# Add ' -x' above to debug
#
# Start an EDDN Service, including redirecting output to a log file.
EXIT_CONFIG_MISSING=1
EXIT_SERVICE_BIN_MISSING=2
EXIT_CL_ARGS=3
EXIT_SERVICE_ALREADY_RUNNING=4
usage() {
echo "Usage: $(basename $0) ( live | beta | dev ) ( gateway | monitor | relay ) [ --from-source [ --background ] ]"
}
if [ -z "${1}" ];
then
usage
echo "No EDDN environment specified."
exit ${EXIT_CL_ARGS}
fi
EDDN_ENV="${1}"
if [ -z "${2}" ];
then
usage
echo "No EDDN service specified."
exit ${EXIT_CL_ARGS}
fi
SERVICE="${2}"
if [ ! -z "${3}" ];
then
if [ "${3}" != "--from-source" ];
then
usage
echo "Un-recognised argument: ${3}"
exit ${EXIT_CL_ARGS}
fi
FROM_SOURCE="1"
if [ ! -z "${4}" ];
then
if [ "${4}" != "--background" ];
then
usage
echo "Un-recognised argument: ${4}"
fi
BACKGROUND=1
fi
fi
EXEC_PATH=$(dirname $0)
#echo "EXEC_PATH: ${EXEC_PATH}"
# Ensure we're in the correct place
cd ${EXEC_PATH}
#pwd
# Bring in some common configuration
if [ ! -f "eddn_${EDDN_ENV}_config" ];
then
echo "eddn_${EDDN_ENV}_config is missing from $(pwd)"
exit ${EXIT_CONFIG_MISSING}
fi
. "./eddn_${EDDN_ENV}_config"
# Use the python venv
. "${PYTHON_VENV}/bin/activate"
# Running from source or install ?
if [ -z "${FROM_SOURCE}" ];
then
# From install
if [ ! -f "${PYTHON_VENV}/bin/eddn-${SERVICE}" ];
then
echo "${SERVICE} is missing from ${PYTHON_VENV}/bin"
exit ${EXIT_SERVICE_BIN_MISSING}
fi
if ps -C python $(cat "${LOG_DIR}/${SERVICE}.pid") > /dev/null 2>&1;
then
echo "${SERVICE}: already running as PID $(cat "${LOG_DIR}/${SERVICE}.pid")"
exit ${EXIT_SERVICE_ALREADY_RUNNING}
fi
${PYTHON_VENV}/bin/eddn-${SERVICE} --config "${CONFIG_OVERRIDE}" >> "${LOG_DIR}/${SERVICE}.log" 2>&1 &
echo $! > "${LOG_DIR}/${SERVICE}.pid"
wait $(cat "${LOG_DIR}/${SERVICE}.pid")
else
# From source
cd "${SRC_DIR}/src/eddn"
#pwd
# For now the source file has Camel case.
#echo ${SERVICE}
SERVICE_CAMEL=$(echo ${SERVICE} | sed -s 's/^\(.\)/\u\1/;')
#echo ${SERVICE_CAMEL}
if [ -z "${BACKGROUND}" ];
then
echo "Starting ${SERVICE_CAMEL} in foreground..."
${PYTHON_VENV}/bin/python ${SERVICE_CAMEL}.py --config "${CONFIG_OVERRIDE}"
else
# We need to ensure there isn't already one, via the .pid file
if [ -f "${LOG_DIR}/${SERVICE}.pid" ];
then
if ps -C python $(cat "${LOG_DIR}/${SERVICE}.pid") > /dev/null 2>&1;
then
echo "${SERVICE}: already running as PID $(cat "${LOG_DIR}/${SERVICE}.pid")"
exit ${EXIT_SERVICE_ALREADY_RUNNING}
fi
fi
echo "Starting ${SERVICE_CAMEL} in background..."
${PYTHON_VENV}/bin/python ${SERVICE_CAMEL}.py --config "${CONFIG_OVERRIDE}" >> "${LOG_DIR}/${SERVICE}.log" 2>&1 &
PID=$!
echo ${PID} > "${LOG_DIR}/${SERVICE}.pid"
echo "${SERVICE_CAMEL}.py started with PID: ${PID}"
fi
fi
# vim: tabstop=2 shiftwidth=2 textwidth=0 wrapmargin=0 expandtab