1#!/bin/sh
2
3DAEMON="fluent-bit"
4PID_FILE="/var/run/$DAEMON.pid"
5CONF_FILE="/etc/$DAEMON/$DAEMON.conf"
6
7FLUENT_BIT_ARGS=""
8
9# shellcheck source=/dev/null
10[ -r "/etc/default/$DAEMON" ] && . "/etc/default/$DAEMON"
11
12start() {
13	printf 'Starting %s: ' "$DAEMON"
14	# shellcheck disable=SC2086 # we need the word splitting
15	start-stop-daemon -S -q -b -m -p "$PID_FILE" --exec "/usr/bin/$DAEMON" \
16		-- -c "$CONF_FILE" $FLUENT_BIT_ARGS
17	status=$?
18	if [ "$status" -eq 0 ]; then
19		echo "OK"
20	else
21		echo "FAIL"
22	fi
23	return "$status"
24}
25
26stop() {
27	printf 'Stopping %s: ' "$DAEMON"
28	start-stop-daemon -K -q -p "$PID_FILE"
29	status=$?
30
31	if [ -f "$PID_FILE" ]; then
32		pid=$(cat "$PID_FILE")
33		rm -f "$PID_FILE"
34
35		# https://docs.fluentbit.io/manual/administration/configuring-fluent-bit/yaml/configuration-file#config_section
36		# The default grace time is set to 5 seconds, so use 6 seconds to have some margin.
37		timeout=6
38		while kill -0 "$pid" 2>/dev/null; do
39			[ $timeout -eq 0 ] && status=1 && break
40			timeout=$((timeout - 1))
41			sleep 1
42		done
43	fi
44
45	if [ "$status" -eq 0 ]; then
46		echo "OK"
47	else
48		echo "FAIL"
49	fi
50	return "$status"
51}
52
53restart() {
54	stop
55	start
56}
57
58case "$1" in
59start)
60	start
61	;;
62stop)
63	stop
64	;;
65restart | reload)
66	restart
67	;;
68*)
69	echo "Usage: $0 {start|stop|restart}"
70	exit 1
71	;;
72esac
73