github.com/stevenmatthewt/agent@v3.5.4+incompatible/packaging/linux/root/usr/share/buildkite-agent/lsb/buildkite-agent.sh (about)

     1  #!/bin/sh
     2  #
     3  ### BEGIN INIT INFO
     4  # Provides:          buildkite-agent
     5  # Required-Start:    $network $local_fs $remote_fs
     6  # Required-Stop:     $remote_fs
     7  # Should-Start:      $named
     8  # Should-Stop:
     9  # Default-Start:     2 3 4 5
    10  # Default-Stop:      0 1 6
    11  # Short-Description: The Buildkite Build Agent
    12  # Description:       The Buildkite Build Agent
    13  ### END INIT INFO
    14  
    15  user="buildkite-agent"
    16  cmd="/usr/bin/buildkite-agent start"
    17  
    18  name=$(basename "$(readlink -f "$0")")
    19  pid_file="/var/run/${name}.pid"
    20  lock_dir="/var/lock/subsys"
    21  lock_file="${lock_dir}/${name}"
    22  log="/var/log/${name}.log"
    23  stderr_log="/var/log/${name}.err"
    24  
    25  [ -r /etc/default/${name} ] && . /etc/default/${name}
    26  [ -r /etc/sysconfig/${name} ] && . /etc/sysconfig/${name}
    27  
    28  get_pid() {
    29      cat "$pid_file"
    30  }
    31  
    32  is_running() {
    33      [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
    34  }
    35  
    36  case "$1" in
    37      start)
    38      if is_running; then
    39          echo "Already started"
    40      else
    41          echo "Starting $name"
    42          su --login --shell /bin/sh "$user" --command "exec $cmd" >>"$log" 2>&1 &
    43          echo $! > "$pid_file"
    44          if ! is_running; then
    45              echo "Unable to start, see $log"
    46              exit 1
    47          fi
    48          if [ -d "$lock_dir" ]; then
    49              touch "$lock_file"
    50          fi
    51      fi
    52      ;;
    53      stop)
    54      if is_running; then
    55          echo -n "Stopping $name.."
    56          kill `get_pid`
    57          for i in {1..10}
    58          do
    59              if ! is_running; then
    60                  break
    61              fi
    62  
    63              echo -n "."
    64              sleep 1
    65          done
    66          echo
    67  
    68          if is_running; then
    69              echo "Not stopped; may still be shutting down or shutdown may have failed"
    70              exit 1
    71          else
    72              echo "Stopped"
    73              if [ -f "$pid_file" ]; then
    74                  rm "$pid_file"
    75              fi
    76              if [ -f "$lock_file" ]; then
    77                  rm -f "$lock_file"
    78              fi
    79          fi
    80      else
    81          echo "Not running"
    82      fi
    83      ;;
    84      restart)
    85      $0 stop
    86      if is_running; then
    87          echo "Unable to stop, will not attempt to start"
    88          exit 1
    89      fi
    90      $0 start
    91      ;;
    92      status)
    93      if is_running; then
    94          echo "Running"
    95      else
    96          echo "Stopped"
    97          exit 1
    98      fi
    99      ;;
   100      *)
   101      echo "Usage: $0 {start|stop|restart|status}"
   102      exit 1
   103      ;;
   104  esac
   105  
   106  exit 0