Skip to content

Instantly share code, notes, and snippets.

@arnabmitra
Created June 5, 2023 22:38
Show Gist options
  • Save arnabmitra/007f17f8a0e33342a9e39d37feea47d8 to your computer and use it in GitHub Desktop.
Save arnabmitra/007f17f8a0e33342a9e39d37feea47d8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if the number of arguments is correct
if [ "$#" -ne 2 ]; then
echo "Usage: ./get_and_watch_logs.sh <namespace> <prefix>"
exit 1
fi
namespace="$1"
prefix="$2"
# Get the pods
pods=$(kubectl get pods -n "$namespace" --no-headers -o custom-columns=":metadata.name")
# Find the pod with the specified prefix
selected_pod=""
while IFS= read -r pod; do
if [[ $pod == "$prefix"* ]]; then
selected_pod=$pod
break
fi
done <<< "$pods"
# Check if the selected pod exists
if [[ -z $selected_pod ]]; then
echo "Pod not found with prefix: $prefix"
exit 1
fi
# Run kubectl logs for the selected pod
kubectl logs "$selected_pod" -n "$namespace" -c "$prefix" -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment