#!/bin/bash
# (Copyright) [2017 - 2017] Confluent, Inc.

#
# Use shellcheck to lint this file
#

# This script is mainly useful if the user has run some non-interactive query via the CLI with the
# -daemon flag passed in; it's less painful than manually grepping for the process and killing it
# However, it comes with the downside that it kills every currently-running CLI session, interactive
# or not, daemon or not:
CLIPIDS=$(ps aux | grep 'io\.confluent\.ksql\.Ksql' | awk '{print $2}')

if [ -z "$CLIPIDS" ]; then
  echo "No CLI session(s) to stop"
  exit 0
fi

for PID in $CLIPIDS; do
  kill -s TERM "$PID"
  while kill -0 "$PID" >/dev/null 2>&1; do sleep 1; done
done
