File: crowdsec-daemon

package info (click to toggle)
crowdsec 1.4.6-10.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,500 kB
  • sloc: sh: 2,870; makefile: 386; python: 74
file content (80 lines) | stat: -rwxr-xr-x 1,612 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash

set -eu
script_name=$0

die() {
    echo >&2 "$@"
    exit 1
}

about() {
    die "usage: ${script_name} [ start | stop ]"
}

#shellcheck disable=SC1007
THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
cd "${THIS_DIR}"/../../
#shellcheck disable=SC1091
. ./.environment.sh

# you have not removed set -u above, have you?

[[ -z "${CROWDSEC-}" ]] && die "\$CROWDSEC must be defined."
[[ -z "${LOG_DIR-}" ]] && die "\$LOG_DIR must be defined."
[[ -z "${PID_DIR-}" ]] && die "\$PID_DIR must be defined."

if [[ ! -f "${CROWDSEC}" ]]; then
    die "${CROWDSEC} is missing. Please build (with 'make bats-build') or install it."
fi

DAEMON_PID=${PID_DIR}/crowdsec.pid

start() {
    daemonize \
        -p "${DAEMON_PID}" \
        -e "${LOG_DIR}/crowdsec.err" \
        -o "${LOG_DIR}/crowdsec.out" \
        "${CROWDSEC}"
    ./bin/wait-for-port 6060
}

start_pid() {
    start
    cat "$DAEMON_PID"
}

stop() {
    if [[ -f "${DAEMON_PID}" ]]; then
       # terminate quickly with extreme prejudice, all the application data will be
       # thrown away anyway. also terminate the child processes (notification plugin).
       PGID="$(ps -o pgid= -p "$(cat "${DAEMON_PID}")" | tr -d ' ')"
       # ps above should work on linux, freebsd, busybox..
       if [[ -n "${PGID}" ]]; then
           kill -- "-${PGID}"
       fi

       rm -f -- "${DAEMON_PID}"
    fi
}


# ---------------------------

[[ $# -lt 1 ]] && about

case "$1" in
    start)
        start
        ;;
    start-pid)
        start_pid
        ;;
    stop)
        stop
        ;;
    *)
        about
        ;;
esac;