-
-
Save Hadryan/78d20e92ffba325ace2d6ed172d81646 to your computer and use it in GitHub Desktop.
Revisions
-
woblerr created this gist
Dec 12, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ # Docker containers logs clearing script ```bash chmod +x ./clear_docker_container_logs.sh ``` ## Usage ```bash Usage: ../clear_docker_container_logs.sh [-c "<container>"] By default, logs for all containers are cleared. -c "<container>" Specific container(s) name or docker-compose service(s) name ``` * Clear all docker logs ```bash $ sudo ./clear_docker_container_logs.sh Clear logs for all containers ``` * Clear logs for nginx ```bash $ sudo ./clear_docker_container_logs.sh -c "nginx" Clear logs for "nginx" ``` * Clear logs for nginx and app (not exist) ```bash $ sudo ./clear_docker_container_logs.sh -c "nginx app" Clear logs for "nginx app" Container or service "app" does not exist. ``` This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,49 @@ #!/bin/bash CLIST="" help() { echo "Usage: $0 [-c \"<container>\"]" echo "" echo "By default, logs for all containers are cleared." echo "" echo -e "\t-c \"<container>\" Specific container(s) name or docker-compose service(s) name" exit 1 } if [ $# == 0 ]; then echo "Clear logs for all containers" CLIST="$(docker ps -aq)" elif [ "$1" == "--" ] || [[ "$1" =~ ^-$ ]] || ! [[ "$1" =~ ^- ]]; then help else while getopts "c:" opt; do case $opt in c) CNAMES+=("$OPTARG") echo "Clear logs for \"$CNAMES\"" ;; \?) help ;; esac done shift $((OPTIND -1)) if ! [ "$1" == "" ]; then help fi fi for i in $CNAMES; do CONTAINERS=$(docker ps -aq -f name=$i 2> /dev/null) if [ "$CONTAINERS" == "" ]; then CONTAINERS="$(docker-compose ps -aq $i 2> /dev/null)" if [ -z $CONTAINERS ]; then echo "Container or service \"$i\" does not exist." fi fi CLIST+=$CONTAINERS$'\n' done for i in $CLIST; do log=$(docker inspect -f '{{.LogPath}}' $i 2> /dev/null) truncate -s 0 $log done