#!/bin/bash # # Make a symlink to _servicer.sh with this syntax: # - XX-myservice-user-type.action # # where: # - XX is a number (ex: 10) # - myservice will be the name of the service (it will create /var/run/myservice.pid for checking service status) # - user is the user the service will run as. Can be omitted, in this case use two "--", and user=service # - type is one of: # - script: the service is managed by a script ($user_home/myservice_start.sh or $user_home/myservice.sh, and $user_home/myservice_stop.sh (optional)) # - service: the service command is specified inside a service_${service}_start file in the user home folder # - action is either start or stop # # LOG_PATH=/var/log/servicer test ! -d "${LOG_PATH}" && mkdir "${LOG_PATH}" actions_logs="${LOG_PATH}/servicer.log" service_user_type_action=${0#*-} SERVICE=${service_user_type_action%%-*} user_type_action=${service_user_type_action#*-} USER=${user_type_action%-*} test -z ${USER} && USER=${SERVICE} type_action=${user_type_action#*-} TYPE=${type_action%.*} ACTION=${type_action#*.} passwd_entry=$(getent passwd ${USER}) || exit 255 USER_HOME=$(echo ${passwd_entry} | cut -d: -f 6) echo "$(date): ${ACTION}-ing service '${SERVICE}' for user '${USER}', as '${TYPE}' (${USER_HOME})" >> ${actions_logs} if [ "${ACTION}" = "start" ] then test -e "${LOG_PATH}/${SERVICE}" || { mkdir "${LOG_PATH}/${SERVICE}" } && chown -R ${USER} "${LOG_PATH}/${SERVICE}" extra_opts=(-1 "${LOG_PATH}/${SERVICE}/${SERVICE}.out.log" -2 "${LOG_PATH}/${SERVICE}/${SERVICE}.err.log") if [ "${TYPE}" = "script" ] then echo " ... checking for start scripts ..." >> ${actions_logs} start_script="${USER_HOME}/${SERVICE}_start.sh" test -e ${start_script} || start_script="${USER_HOME}/${SERVICE}.sh" echo " ... detected '${start_script}' ..." >> ${actions_logs} COMMAND="${start_script}" ARGUMENTS="" elif [ "${TYPE}" = "service" ] then echo " ... checking for config settings ..." >> ${actions_logs} source_script="${USER_HOME}/service_${SERVICE}_start" test -e "${source_script}" || { echo "Error, missing '${source_script}'" >> ${actions_logs} exit 255 } echo " ... sourcing '${source_script}'..." >> ${actions_logs} source "${source_script}" fi action=(-b -m --start "${COMMAND}" -- ${ARGUMENTS[@]}) elif [ "${ACTION}" = "stop" ] then extra_opts=() if [ "${TYPE}" = "script" ] then echo " ... checking for stop script ..." >> ${actions_logs} stop_script="${USER_HOME}/${SERVICE}_stop.sh" test -e "${stop_script}" && { echo " ... running '${stop_script}' ..." >> ${actions_logs} su - ${USER} -c "${stop_script}" } elif [ "${TYPE}" = "service" ] then true fi action=(--stop ${SERVICE}) fi echo start-stop-daemon -p /var/run/${SERVICE}.pid ${extra_opts[@]} -u ${USER} -d ${USER_HOME} ${action[@]} >> ${actions_logs} start-stop-daemon -p /var/run/${SERVICE}.pid ${extra_opts[@]} -u ${USER} -d ${USER_HOME} ${action[@]} echo "$(date): ${ACTION}-ed service '${SERVICE}' for user '${USER}', as '${TYPE}' (${USER_HOME})" >> ${actions_logs}