github.com/craicoverflow/tyk@v2.9.6-rc3+incompatible/install/inits/sysv/init.d/tyk-gateway (about)

     1  #!/bin/sh
     2  # Init script for tyk-gateway
     3  # Maintained by 
     4  # Generated by pleaserun.
     5  # Implemented based on LSB Core 3.1:
     6  #   * Sections: 20.2, 20.3
     7  #
     8  ### BEGIN INIT INFO
     9  # Provides:          tyk-gateway
    10  # Required-Start:    $remote_fs $syslog
    11  # Required-Stop:     $remote_fs $syslog
    12  # Default-Start:     2 3 4 5
    13  # Default-Stop:      0 1 6
    14  # Short-Description: 
    15  # Description:       Tyk API Gateway
    16  ### END INIT INFO
    17  
    18  PATH=/sbin:/usr/sbin:/bin:/usr/bin
    19  export PATH
    20  
    21  name=tyk-gateway
    22  program=/opt/tyk-gateway/tyk
    23  args='--conf /opt/tyk-gateway/tyk.conf'
    24  pidfile="/var/run/tyk-gateway.pid"
    25  
    26  [ -r /etc/default/$name ] && . /etc/default/$name
    27  [ -r /etc/sysconfig/$name ] && . /etc/sysconfig/$name
    28  
    29  trace() {
    30    logger -t "/etc/init.d/tyk-gateway" "$@"
    31  }
    32  
    33  emit() {
    34    trace "$@"
    35    echo "$@"
    36  }
    37  
    38  start() {
    39    # Setup any environmental stuff beforehand
    40    
    41  
    42    # Run the program!
    43    
    44    chroot --userspec "$user":"$group" "$chroot" sh -c "
    45      
    46      cd \"$chdir\"
    47      exec \"$program\" $args
    48    " >> /var/log/tyk-gateway.stdout 2>> /var/log/tyk-gateway.stderr &
    49  
    50    # Generate the pidfile from here. If we instead made the forked process
    51    # generate it there will be a race condition between the pidfile writing
    52    # and a process possibly asking for status.
    53    echo $! > $pidfile
    54  
    55    emit "$name started"
    56    return 0
    57  }
    58  
    59  stop() {
    60    # Try a few times to kill TERM the program
    61    if status ; then
    62      pid=$(cat "$pidfile")
    63      trace "Killing $name (pid $pid) with SIGTERM"
    64      kill -TERM $pid
    65      # Wait for it to exit.
    66      for i in 1 2 3 4 5 ; do
    67        trace "Waiting $name (pid $pid) to die..."
    68        status || break
    69        sleep 1
    70      done
    71      if status ; then
    72        emit "$name stop failed; still running."
    73      else
    74        emit "$name stopped."
    75      fi
    76    fi
    77  }
    78  
    79  status() {
    80    if [ -f "$pidfile" ] ; then
    81      pid=$(cat "$pidfile")
    82      if ps -p $pid > /dev/null 2> /dev/null ; then
    83        # process by this pid is running.
    84        # It may not be our pid, but that's what you get with just pidfiles.
    85        # TODO(sissel): Check if this process seems to be the same as the one we
    86        # expect. It'd be nice to use flock here, but flock uses fork, not exec,
    87        # so it makes it quite awkward to use in this case.
    88        return 0
    89      else
    90        return 2 # program is dead but pid file exists
    91      fi
    92    else
    93      return 3 # program is not running
    94    fi
    95  }
    96  
    97  force_stop() {
    98    if status ; then
    99      stop
   100      status && kill -KILL $(cat "$pidfile")
   101    fi
   102  }
   103  
   104  
   105  case "$1" in
   106    force-start|start|stop|force-stop|restart)
   107      trace "Attempting '$1' on tyk-gateway"
   108      ;;
   109  esac
   110  
   111  case "$1" in
   112    force-start)
   113      PRESTART=no
   114      exec "$0" start
   115      ;;
   116    start)
   117      status
   118      code=$?
   119      if [ $code -eq 0 ]; then
   120        emit "$name is already running"
   121        exit $code
   122      else
   123        start
   124        exit $?
   125      fi
   126      ;;
   127    stop) stop ;;
   128    force-stop) force_stop ;;
   129    status) 
   130      status
   131      code=$?
   132      if [ $code -eq 0 ] ; then
   133        emit "$name is running"
   134      else
   135        emit "$name is not running"
   136      fi
   137      exit $code
   138      ;;
   139    restart) 
   140      
   141      stop && start 
   142      ;;
   143    *)
   144      echo "Usage: $SCRIPTNAME {start|force-start|stop|force-start|force-stop|status|restart}" >&2
   145      exit 3
   146    ;;
   147  esac
   148  
   149  exit $?