Created
June 5, 2023 22:38
-
-
Save arnabmitra/007f17f8a0e33342a9e39d37feea47d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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