github.com/crowdsecurity/crowdsec@v1.6.1/test/lib/init/crowdsec-systemd (about)

     1  #!/usr/bin/env bash
     2  
     3  set -eu
     4  script_name=$0
     5  
     6  die() {
     7      echo >&2 "$@"
     8      exit 1
     9  }
    10  
    11  about() {
    12      die "usage: ${script_name} [ start | stop ]"
    13  }
    14  
    15  #shellcheck disable=SC1007
    16  THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
    17  cd "${THIS_DIR}"/../../
    18  #shellcheck disable=SC1091
    19  . ./.environment.sh
    20  
    21  # you have not removed set -u above, have you?
    22  
    23  [[ -z "${CROWDSEC-}" ]] && die "\$CROWDSEC must be defined."
    24  [[ -z "${CSCLI-}" ]] && die "\$CSCLI must be defined."
    25  [[ -z "${LOG_DIR-}" ]] && die "\$LOG_DIR must be defined."
    26  [[ -z "${PID_DIR-}" ]] && die "\$PID_DIR must be defined."
    27  
    28  
    29  if [[ ! -f "${CROWDSEC}" ]]; then
    30      die "${CROWDSEC} is missing. Please build (with 'make bats-build') or install it."
    31  fi
    32  
    33  start() {
    34      systemctl start crowdsec
    35      ./bin/wait-for-port 6060
    36  }
    37  
    38  start_pid() {
    39      start
    40      pidof /usr/bin/crowdsec
    41  }
    42  
    43  stop() {
    44      systemctl stop crowdsec # systemd doesn't throw error when stopping already stopped stuff
    45      while pidof /usr/bin/crowdsec ; do sleep 0.1; done
    46  }
    47  
    48  
    49  # ---------------------------
    50  
    51  [[ $# -lt 1 ]] && about
    52  
    53  case "$1" in
    54      start)
    55          start
    56          ;;
    57      start-pid)
    58          start_pid
    59          ;;
    60      stop)
    61          stop
    62          ;;
    63      *)
    64          about
    65          ;;
    66  esac;
    67