Tomáš Pecka | be79707 | 2022-07-15 20:45:52 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | PIDFILE="./test-alarms.pid" |
| 4 | |
| 5 | set -x |
| 6 | |
| 7 | stop() { |
| 8 | [[ ! -r "$PIDFILE" ]] && return 0 # no pidfile |
| 9 | |
| 10 | PID="$(cat "$PIDFILE")" |
| 11 | |
| 12 | if ps --pid "$PID" >/dev/null; then |
| 13 | kill -15 -- "$PID" 2>/dev/null # please terminate |
| 14 | sleep 0.5 |
| 15 | while ps --pid "$PID" >/dev/null; do |
| 16 | kill -9 -- "$PID" 2>/dev/null # shots fired |
| 17 | done |
| 18 | fi |
| 19 | echo "" > "$PIDFILE" |
| 20 | } |
| 21 | |
| 22 | # $1 ... path to daemon |
| 23 | start() { |
| 24 | rm -f "$PIDFILE" # in case these files already exist |
| 25 | |
| 26 | if [[ ! -x "$1" ]]; then |
| 27 | echo "$1 is not executable" >&2 |
| 28 | return 1 |
| 29 | fi |
| 30 | |
Tomáš Pecka | 04cd95d | 2023-05-11 20:10:04 +0200 | [diff] [blame^] | 31 | $1 --log-level=5 --sysrepo-log-level=5 1>$2 2>&1 & |
Tomáš Pecka | be79707 | 2022-07-15 20:45:52 +0200 | [diff] [blame] | 32 | PID="$!" |
| 33 | echo "$PID" > $PIDFILE |
| 34 | |
| 35 | # wait for init |
| 36 | sleep 2 |
| 37 | |
| 38 | echo "Started $1 (pid=$PID)" >&2 |
| 39 | } |
| 40 | |
| 41 | help() { |
| 42 | echo "Usage:" >&2 |
| 43 | echo " $1 start PATH_TO_SYSREPO_IETF_ALARMSD LOGFILE" >&2 |
| 44 | echo " $1 stop" >&2 |
| 45 | } |
| 46 | |
| 47 | if [[ "$1" == "start" && $# == 3 ]]; then |
| 48 | stop |
| 49 | start "$2" "$3" |
| 50 | elif [[ "$1" == "stop" && $# == 1 ]]; then |
| 51 | stop |
| 52 | else |
| 53 | help |
| 54 | exit 1 |
| 55 | fi |