Created
August 29, 2018 14:05
-
-
Save efrecon/64ec348f9a1089b7c7714752b41a6c4d to your computer and use it in GitHub Desktop.
Execut docker log on all containers at once
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 | |
names=$(docker ps --format "{{.Names}}") | |
echo "tailing $names" | |
while read -r name | |
do | |
# eval to show container name in jobs list | |
eval "docker logs -f --tail=5 \"$name\" | sed -e \"s/^/[-- $name --] /\" &" | |
# For Ubuntu 16.04 | |
#eval "docker logs -f --tail=5 \"$name\" |& sed -e \"s/^/[-- $name --] /\" &" | |
done <<< "$names" | |
function _exit { | |
echo | |
echo "Stopping tails $(jobs -p | tr '\n' ' ')" | |
echo "..." | |
# Using `sh -c` so that if some have exited, that error will | |
# not prevent further tails from being killed. | |
jobs -p | tr '\n' ' ' | xargs -I % sh -c "kill % || true" | |
echo "Done" | |
} | |
# On ctrl+c, kill all tails started by this script. | |
trap _exit EXIT | |
# For Ubuntu 16.04 | |
#trap _exit INT | |
# Don't exit this script until ctrl+c or all tails exit. | |
wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is shamelessly taken from https://stackoverflow.com/a/38308974