如何让logstash作为服务在Ubuntu 12.04上运行,该进程由root以外的用户拥有?

时间:2015-09-29 18:35:31

标签: ubuntu logstash init.d

我试图让logstash作为root用户以外的用户所拥有的服务运行。 init.d脚本如下:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          tlogserver
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Log Server
# Description:       Talend Logstash Service
### END INIT INFO
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/sbin:/usr/bin:/sbin:/bin

if [ "X${TALEND_LOGSERV}" = "X" ]; then
  TALEND_LOGSERV="/opt/talend/logserv"
fi

if [ "X${TALEND_RUN}" = "X" ]; then
  TALEND_RUN="/opt/talend/run"
fi

DESC="Logstash service for the TAC"
APP_NAME="tlogserver"
DAEMON_START="${TALEND_LOGSERV}/start_logserver.sh"
DAEMON_START_ARGS=""
DAEMON_STOP="${TALEND_LOGSERV}/stop_logserver.sh"
DAEMON_STOP_ARGS=""
PIDFILE="${TALEND_RUN}/${APP_NAME}.pid"
RUN_AS="tomcat7"
RUN_GRP="tomcat7"
SCRIPTNAME="/etc/init.d/${APP_NAME}"

# Exit if the package is not installed
[ -x "${DAEMON_START}" ] || "The daemon is not installed"

# Read configuration variable file if it is present
[ -r /etc/default/${APP_NAME} ] && . /etc/default/${APP_NAME}

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start() {
  start-stop-daemon --start --quiet --background --chuid ${RUN_AS}:${RUN_GRP} --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON_START} --test > /dev/null \
    || return 1
  start-stop-daemon --start --quiet --background --chuid ${RUN_AS}:${RUN_GRP} --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON_START} -- \
    ${DAEMON_ARGS} \
    || return 2
}

#
# Function that stops the daemon/service
#
do_stop() {
  start-stop-daemon --start --quiet --background --chuid ${RUN_AS}:${RUN_GRP} --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON_STOP} --test > /dev/null \
    || return 1
  start-stop-daemon --start --quiet --background --chuid ${RUN_AS}:${RUN_GRP} --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON_STOP} -- \
    ${DAEMON_ARGS} \
    || return 2
}

case "$1" in
  start)
    [ "${VERBOSE}" != no ] && log_daemon_msg "Starting ${DESC}" "${APP_NAME}"
    do_start
    case "$?" in
      0|1) [ "${VERBOSE}" != no ] && log_end_msg 0 ;;
      2) [ "${VERBOSE}" != no ] && log_end_msg 1 ;;
    esac
    ;;
  stop)
    [ "${VERBOSE}" != no ] && log_daemon_msg "Stopping ${DESC}" "${APP_NAME}"
    do_stop
    case "$?" in
      0|1) [ "${VERBOSE}" != no ] && log_end_msg 0 ;;
      2) [ "${VERBOSE}" != no ] && log_end_msg 1 ;;
    esac
    ;;
  restart|force-reload)
    #
    # If the "reload" option is implemented then remove the
    # 'force-reload' alias
    #
    log_daemon_msg "Restarting ${DESC}" "${APP_NAME}"
    do_stop
    case "$?" in
      0|1)
        do_start
        case "$?" in
          0) log_end_msg 0 ;;
          1) log_end_msg 1 ;; # Old process is still running
          *) log_end_msg 1 ;; # Failed to start
        esac
        ;;
      *)
        # Failed to stop
        log_end_msg 1
        ;;
    esac
    ;;
  *)
    echo "Usage: ${SCRIPTNAME} {start|stop|restart}" >&2
    exit 3
    ;;
esac

当我将其作为服务运行时,我获得了成功的返回码,但是当我查找它时,logstash进程没有运行。 start_logserver脚本如下:

if [ "X${TALEND_LOGSERV}" = "X" ]; then
  TALEND_LOGSERV="/opt/talend/logserv"
fi

cd "$TALEND_LOGSERV"
# ./logstash-1.4.2/bin/logstash agent -f logstash-talend.conf -l /var/log/talend/logserv.log > /dev/null 2>&1 < /dev/null &
echo "inside the start script...." >&2
echo `pwd` >&2
./logstash-1.4.2/bin/logstash agent -f logstash-talend.conf -l /var/log/talend/logserv.log
echo $!

2 个答案:

答案 0 :(得分:0)

可以为logstash / sysv / upstart生成systemd的服务脚本,其中包含:

LS_HOME=/usr/share/logstash

# use ONE of the following:
${LS_HOME}/bin/system-install ${LS_HOME}/config/startup.options sysv
${LS_HOME}/bin/system-install ${LS_HOME}/config/startup.options upstart
${LS_HOME}/bin/system-install ${LS_HOME}/config/startup.options systemd

这些脚本默认以logstash:logstash运行。

答案 1 :(得分:-1)

@Magnus在评论中提供了一个非常有效的链接(elastic/logstash/pkg)。他最初的评论是:

  

您是否有任何理由没有使用init script that ships with Logstash作为起点?另外,您是否尝试使用-x运行这些shell脚本以列出所有运行的命令?作为最后的评论,start-stop-daemon捕获的pid不会是shell脚本而不是Logstash进程吗?