mydocker/truncate_logs.sh

49 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# Ustawienia domyślne
DEFAULT_SIZE=555
# Sprawdzenie argumentów
if [[ $# -eq 0 ]]; then
echo "Usage: $0 <container_name> [log_size_in_bytes]"
exit 1
fi
container=$1
size=${2:-$DEFAULT_SIZE} # Użyj podanego rozmiaru lub domyślnego
# Znajdowanie ścieżki do pliku logów
file=$(docker inspect --format='{{.LogPath}}' "$container" 2>/dev/null)
if [[ $? -ne 0 || -z "$file" ]]; then
echo "Error: Could not find logs for container '$container'."
exit 2
fi
# Upewnij się, że plik istnieje
if ! sudo test -f "$file"; then
echo "Error: Log file '$file' does not exist or cannot be accessed. Check permissions."
exit 3
fi
# Przycinanie pliku logów
echo "Truncating log file '$file' to $size bytes..."
sudo truncate -s "$size" "$file" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "Error: Failed to truncate log file '$file'. Check permissions."
exit 4
fi
# Wymuszenie przeładowania logów w Dockerze
echo "Signaling Docker to reload logs for container '$container'..."
docker kill --signal=SIGHUP "$container" > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo "Logs for container '$container' truncated to $size bytes and reloaded successfully."
else
echo "Error: Failed to reload logs for container '$container'. Check Docker status."
exit 5
fi