11#! /bin/sh
22
3- # This file is run on successful log rotation
3+ if [ -z " ${LOG_PATH:- } " ]; then
4+ echo " LOG_PATH is not set."
5+ exit 1
6+ fi
47
5- # Define the log file and the rotated log file
6- ROTATED_LOG_FILE=" $LOG_PATH .1"
8+ # If from logrotate, the file to export is the rotated file ( .1 ), otherwise it's the current log file ( .log )
9+ if [ " ${FROM_LOGROTATE:- } " = " 1" ]; then
10+ RUN_MODE=" logrotate"
11+ EXPORT_LOG_FILE=" $LOG_PATH .1"
12+ else
13+ RUN_MODE=" manual"
14+ EXPORT_LOG_FILE=" $LOG_PATH "
15+ fi
716
817# Check if the rotated log file exists
9- if [ -f " $ROTATED_LOG_FILE " ]; then
10- echo " Log rotation successful. Processing the rotated log file."
18+ if [ -f " $EXPORT_LOG_FILE " ]; then
19+ if [ " $RUN_MODE " = " logrotate" ]; then
20+ # Only needed after rotation, so traefik reopens the active log file.
21+ TRAEFIK_PID=$( pgrep traefik)
1122
12- # Find the PID of the traefik process
13- TRAEFIK_PID=$( pgrep traefik)
23+ if [ -n " $TRAEFIK_PID " ]; then
24+ kill -USR1 " $TRAEFIK_PID "
25+ echo " Sent USR1 signal to traefik process with PID $TRAEFIK_PID "
26+ else
27+ echo " Traefik process not found."
28+ fi
1429
15- # Check if the PID was found
16- if [ -n " $TRAEFIK_PID " ]; then
17- # Send USR1 signal to the traefik process
18- kill -USR1 " $TRAEFIK_PID "
19- echo " Sent USR1 signal to traefik process with PID $TRAEFIK_PID "
20- else
21- echo " Traefik process not found."
30+ # Give traefik time to reopen before continuing.
31+ sleep 10
2232 fi
2333
24- # Make sure traefik has time to reopen the log file before we try to compress it
25- sleep 10
26-
2734 # Get the uploaded log file name
2835 TIMESTAMP=$( date +" %Y%m%dT%H00Z" ) # Cron runs on the hour, so we round to the hour
29- HASH_SUFFIX=$( cksum " $ROTATED_LOG_FILE " | cut -d ' ' -f 1 | cut -c1-8)
36+ HASH_SUFFIX=$( cksum " $EXPORT_LOG_FILE " | cut -d ' ' -f 1 | cut -c1-8)
3037 S3_KEY=" ${TIMESTAMP} _HASH${HASH_SUFFIX} _access.log.zst"
3138
3239 # Compress the rotated log before upload
33- if ! zstd -19 -q -c " $ROTATED_LOG_FILE " > " $S3_KEY " ; then
40+ if ! zstd -19 -q -c " $EXPORT_LOG_FILE " > " $S3_KEY " ; then
3441 echo " Failed to compress rotated log file."
3542 exit 1
3643 fi
@@ -49,6 +56,10 @@ if [ -f "$ROTATED_LOG_FILE" ]; then
4956
5057 echo " Upload to S3 successful."
5158else
52- echo " Rotated log file not found."
59+ if [ " $RUN_MODE " = " logrotate" ]; then
60+ echo " Rotated log file not found: $EXPORT_LOG_FILE "
61+ else
62+ echo " Log file not found: $EXPORT_LOG_FILE "
63+ fi
5364 exit 1
5465fi
0 commit comments