Skip to content

Instantly share code, notes, and snippets.

@adkinss
Created September 11, 2020 13:40
Show Gist options
  • Save adkinss/11a0e220bc996ecc2a6fd98747f3e348 to your computer and use it in GitHub Desktop.
Save adkinss/11a0e220bc996ecc2a6fd98747f3e348 to your computer and use it in GitHub Desktop.
Method for logging to hourly and daily files, including symlinks to the latest log of each.
#!/bin/bash
# Technique:
#
# - Run the script via cron hourly. Be mindful to stagger cron jobs and not put them all at the top of the hour.
# - Whatever the current hour is (00 - 23), that is the log file that will be written too.
# - Run the log collection script and save it to the current hour log file.
# - Relink the latest hourly symlink file to the newly created current hour log file.
# - Check to see if the daily log exists. Reasons for it NOT existing include:
# - Current time crossed midnight and it is now a new day.
# - A reboot, system crash, or maintenance resulted in one or more cron job runs being skipped.
# - If the daily log does not exist, copy the current hourly log to the daily log. Do not link it... copy it.
# - Relink the latest daily symlink file to the newly created current daily log file.
#
# Sample directory listing for logs collected in September 2020 (cron job runs at 6 minutes past each hour):
#
# -rw-r--r-- 1 thisuser 3783 Sep 1 00:06 data_2020-09-01.log
# -rw-r--r-- 1 thisuser 3784 Sep 2 00:06 data_2020-09-02.log
# -rw-r--r-- 1 thisuser 3788 Sep 3 00:06 data_2020-09-03.log
# -rw-r--r-- 1 thisuser 3784 Sep 4 00:06 data_2020-09-04.log
# -rw-r--r-- 1 thisuser 3784 Sep 5 00:06 data_2020-09-05.log
# -rw-r--r-- 1 thisuser 3784 Sep 6 00:06 data_2020-09-06.log
# -rw-r--r-- 1 thisuser 3784 Sep 7 00:06 data_2020-09-07.log
# -rw-r--r-- 1 thisuser 3734 Sep 8 00:06 data_2020-09-08.log
# -rw-r--r-- 1 thisuser 3750 Sep 9 00:06 data_2020-09-09.log
# -rw-r--r-- 1 thisuser 3748 Sep 10 00:06 data_2020-09-10.log
# -rw-r--r-- 1 thisuser 3755 Sep 10 13:06 data_hourly_13.log
# -rw-r--r-- 1 thisuser 3755 Sep 10 14:06 data_hourly_14.log
# -rw-r--r-- 1 thisuser 3755 Sep 10 15:06 data_hourly_15.log
# -rw-r--r-- 1 thisuser 3755 Sep 10 16:06 data_hourly_16.log
# -rw-r--r-- 1 thisuser 3755 Sep 10 17:06 data_hourly_17.log
# -rw-r--r-- 1 thisuser 3755 Sep 10 18:06 data_hourly_18.log
# -rw-r--r-- 1 thisuser 3755 Sep 10 19:06 data_hourly_19.log
# -rw-r--r-- 1 thisuser 3756 Sep 10 20:06 data_hourly_20.log
# -rw-r--r-- 1 thisuser 3758 Sep 10 21:06 data_hourly_21.log
# -rw-r--r-- 1 thisuser 3758 Sep 10 22:06 data_hourly_22.log
# -rw-r--r-- 1 thisuser 3758 Sep 10 23:06 data_hourly_23.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 00:06 data_hourly_00.log
# lrwxrwxrwx 1 thisuser 22 Sep 11 00:06 data_latest.log -> data_2020-09-11.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 00:06 data_2020-09-11_00.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 01:06 data_hourly_01.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 02:06 data_hourly_02.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 03:06 data_hourly_03.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 04:06 data_hourly_04.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 05:06 data_hourly_05.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 06:06 data_hourly_06.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 07:06 data_hourly_07.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 08:06 data_hourly_08.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 09:06 data_hourly_09.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 10:06 data_hourly_10.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 11:06 data_hourly_11.log
# lrwxrwxrwx 1 thisuser 18 Sep 11 12:06 data_hourly_latest.log -> data_hourly_12.log
# -rw-r--r-- 1 thisuser 3758 Sep 11 12:06 data_hourly_12.log
BASENAME="data" # The base name for all the log files.
DATADIR="/data/logs" # Where the log files get written to.
SCRIPT="logger" # The data collection script that outputs log data.
HOUR=$(date +%H)
HOURLY_FILE=${BASENAME}_hourly_${HOUR}.log
HOURLY_LATEST=${BASENAME}_hourly_latest.log
cd $DATADIR
$SCRIPT > $HOURLY_HOUR
ln -sf $HOURLY_HOUR $HOURLY_LATEST
DAY=$(date +%Y-%m-%d)
DAILY_FILE=${BASENAME}_${DAY}.log
DAILY_LATEST=${BASENAME}_latest.log
if [[ ! -f $DAILY_FILE ]]; then
cp -p $HOURLY_FILE $DAILY_FILE
ln -sf $DAILY_FILE $DAILY_LATEST
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment