~verterok/charms/trusty/logstash-forwarder/focal-support

« back to all changes in this revision

Viewing changes to files/init.sh

  • Committer: Michael Foley
  • Date: 2015-05-22 03:31:40 UTC
  • Revision ID: michael.foley@canonical.com-20150522033140-tno5otdn046lhytc
[foli] New version of logstash-forwarder deb and new init script to fix starting twice

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /bin/sh
 
1
#!/bin/sh
 
2
# Init script for logstash-forwarder
 
3
# Maintained by 
 
4
# Generated by pleaserun.
 
5
# Implemented based on LSB Core 3.1:
 
6
#   * Sections: 20.2, 20.3
 
7
#
2
8
### BEGIN INIT INFO
3
 
# Provides:          skeleton
 
9
# Provides:          logstash-forwarder
4
10
# Required-Start:    $remote_fs $syslog
5
11
# Required-Stop:     $remote_fs $syslog
6
12
# Default-Start:     2 3 4 5
7
13
# Default-Stop:      0 1 6
8
 
# Short-Description: Example initscript
9
 
# Description:       This file should be used to construct scripts to be
10
 
#                    placed in /etc/init.d.
 
14
# Short-Description: 
 
15
# Description:       no description given
11
16
### END INIT INFO
12
17
 
13
 
# Author: Jordan Sissel <jordan.sissel@dreamhost.com>
14
 
 
15
 
# PATH should only include /usr/* if it runs after the mountnfs.sh script
16
18
PATH=/sbin:/usr/sbin:/bin:/usr/bin
17
 
DESC="log shipper"
18
 
NAME=logstash-forwarder
19
 
DAEMON=/srv/logstash-forwarder/bin/logstash-forwarder
20
 
DAEMON_ARGS="-config /etc/logstash-forwarder -spool-size 100"
21
 
PIDFILE=/var/run/$NAME.pid
22
 
SCRIPTNAME=/etc/init.d/$NAME
23
 
LOGDIR=/var/log/logstash-forwarder
24
 
 
25
 
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
26
 
. /lib/init/vars.sh
27
 
. /lib/lsb/init-functions
28
 
 
29
 
if [ ! -d ${LOGDIR} ]; then
30
 
    mkdir -p ${LOGDIR}
31
 
fi
32
 
COMMAND="cd /var/run; exec $DAEMON $DAEMON_ARGS >> ${LOGDIR}/logstash-forwarder.log 2>&1"
33
 
 
34
 
do_start() {
35
 
  # Skip if it's already running
36
 
  start-stop-daemon --start --quiet --pidfile $PIDFILE --exec /bin/sh --test > /dev/null || return 1
37
 
 
38
 
  cd /var/run
39
 
  # Actually start it now.
40
 
  start-stop-daemon --start --quiet --make-pidfile --background \
41
 
    --pidfile $PIDFILE --exec /bin/sh -- -c "$COMMAND" || return 2
42
 
}
43
 
 
44
 
do_stop()
45
 
{
46
 
  start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
47
 
  RETVAL="$?"
48
 
  [ "$RETVAL" = 2 ] && return 2
49
 
  start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
50
 
  [ "$?" = 2 ] && return 2
51
 
  rm -f $PIDFILE
52
 
  return "$RETVAL"
53
 
}
54
 
 
55
 
case "$1" in
 
19
export PATH
 
20
 
 
21
name=logstash-forwarder
 
22
program=/srv/logstash-forwarder/bin/logstash-forwarder
 
23
pidfile="/var/run/$name.pid"
 
24
 
 
25
[ -r /etc/default/$name ] && . /etc/default/$name
 
26
[ -r /etc/sysconfig/$name ] && . /etc/sysconfig/$name
 
27
args=$args
 
28
 
 
29
trace() {
 
30
  logger -t "/etc/init.d/logstash-forwarder" "$@"
 
31
}
 
32
 
 
33
emit() {
 
34
  trace "$@"
 
35
  echo "$@"
 
36
}
 
37
 
 
38
start() {
 
39
 
 
40
  # Ensure the log directory is setup correctly.
 
41
  [ ! -d "/var/log/logstash-forwarder/" ] && mkdir "/var/log/logstash-forwarder/"
 
42
  chown "$user":"$group" "/var/log/logstash-forwarder/"
 
43
  chmod 755 "/var/log/logstash-forwarder/"
 
44
 
 
45
 
 
46
  # Setup any environmental stuff beforehand
 
47
  
 
48
 
 
49
  # Run the program!
 
50
  
 
51
  chroot --userspec "$user":"$group" "$chroot" sh -c "
 
52
    
 
53
    cd \"$chdir\"
 
54
    exec \"$program\" $args
 
55
  " >> /var/log/logstash-forwarder/logstash-forwarder.stdout 2>> /var/log/logstash-forwarder/logstash-forwarder.stderr &
 
56
 
 
57
  # Generate the pidfile from here. If we instead made the forked process
 
58
  # generate it there will be a race condition between the pidfile writing
 
59
  # and a process possibly asking for status.
 
60
  echo $! > $pidfile
 
61
 
 
62
  emit "$name started"
 
63
  return 0
 
64
}
 
65
 
 
66
stop() {
 
67
  # Try a few times to kill TERM the program
 
68
  if status ; then
 
69
    pid=$(cat "$pidfile")
 
70
    trace "Killing $name (pid $pid) with SIGTERM"
 
71
    kill -TERM $pid
 
72
    # Wait for it to exit.
 
73
    for i in 1 2 3 4 5 ; do
 
74
      trace "Waiting $name (pid $pid) to die..."
 
75
      status || break
 
76
      sleep 1
 
77
    done
 
78
    if status ; then
 
79
      emit "$name stop failed; still running."
 
80
    else
 
81
      emit "$name stopped."
 
82
    fi
 
83
  fi
 
84
}
 
85
 
 
86
status() {
 
87
  if [ -f "$pidfile" ] ; then
 
88
    pid=$(cat "$pidfile")
 
89
    if ps -p $pid > /dev/null 2> /dev/null ; then
 
90
      # process by this pid is running.
 
91
      # It may not be our pid, but that's what you get with just pidfiles.
 
92
      # TODO(sissel): Check if this process seems to be the same as the one we
 
93
      # expect. It'd be nice to use flock here, but flock uses fork, not exec,
 
94
      # so it makes it quite awkward to use in this case.
 
95
      return 0
 
96
    else
 
97
      return 2 # program is dead but pid file exists
 
98
    fi
 
99
  else
 
100
    return 3 # program is not running
 
101
  fi
 
102
}
 
103
 
 
104
force_stop() {
 
105
  if status ; then
 
106
    stop
 
107
    status && kill -KILL $(cat "$pidfile")
 
108
  fi
 
109
}
 
110
 
 
111
 
 
112
case "$1" in
 
113
  force-start|start|stop|force-stop|restart)
 
114
    trace "Attempting '$1' on logstash-forwarder"
 
115
    ;;
 
116
esac
 
117
 
 
118
case "$1" in
 
119
  force-start)
 
120
    PRESTART=no
 
121
    exec "$0" start
 
122
    ;;
56
123
  start)
57
 
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
58
 
    do_start
59
 
    case "$?" in
60
 
      0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
61
 
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
62
 
    esac
63
 
    ;;
64
 
  stop)
65
 
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
66
 
    do_stop
67
 
    case "$?" in
68
 
      0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
69
 
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
70
 
    esac
71
 
    ;;
72
 
  status)
73
 
    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
74
 
    ;;
75
 
  restart|force-reload)
76
 
    log_daemon_msg "Restarting $DESC" "$NAME"
77
 
    do_stop
78
 
    case "$?" in
79
 
      0|1)
80
 
        do_start
81
 
        case "$?" in
82
 
          0) log_end_msg 0 ;;
83
 
          1) log_end_msg 1 ;; # Old process is still running
84
 
          *) log_end_msg 1 ;; # Failed to start
85
 
        esac
86
 
      ;;
87
 
      *)
88
 
        # Failed to stop
89
 
        log_end_msg 1
90
 
      ;;
91
 
    esac
 
124
    status
 
125
    code=$?
 
126
    if [ $code -eq 0 ]; then
 
127
      emit "$name is already running"
 
128
      exit $code
 
129
    else
 
130
      start
 
131
      exit $?
 
132
    fi
 
133
    ;;
 
134
  stop) stop ;;
 
135
  force-stop) force_stop ;;
 
136
  status) 
 
137
    status
 
138
    code=$?
 
139
    if [ $code -eq 0 ] ; then
 
140
      emit "$name is running"
 
141
    else
 
142
      emit "$name is not running"
 
143
    fi
 
144
    exit $code
 
145
    ;;
 
146
  restart) 
 
147
    
 
148
    stop && start 
92
149
    ;;
93
150
  *)
94
 
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
 
151
    echo "Usage: $SCRIPTNAME {start|force-start|stop|force-start|force-stop|status|restart}" >&2
95
152
    exit 3
96
 
    ;;
 
153
  ;;
97
154
esac
98
155
 
99
 
:
 
156
exit $?