Sunday, October 24, 2021

[SOLVED] How to Check whether the date changed in linux

Issue

I wrote a script to capture ngrep log daily. But it's not creating file automatically when the date has been changed.

#!/bin/bash

month=$( date +%B )
mkdir -p /home/log/$month

NOW=$(date '+%Y%m%d')
LOGFILE1="/home/log/$month/5110_$NOW.txt"
LOGFILE2="/home/log/$month/5150_$NOW.txt"
LOGFILE3="/home/log/$month/5160_$NOW.txt"

while true
do
  ngrep -t -q -d any -W byline port 5110 >> $LOGFILE1 &
  ngrep -t -q -d any -W byline port 5150 >> $LOGFILE2 &
  ngrep -t -q -d any -W byline port 5160 >> $LOGFILE3
  exec bash
  sleep 2
done

Please help.

Thanks


Solution

You would have to move the variable definitions into the while loop for that to happen.

#!/bin/bash
while true
do    
month=$( date +%B )
mkdir -p /home/log/$month


NOW=$(date '+%Y%m%d')
LOGFILE1="/home/log/$month/5110_$NOW.txt"
LOGFILE2="/home/log/$month/5150_$NOW.txt"
LOGFILE3="/home/log/$month/5160_$NOW.txt"

ngrep -t -q -d any -W byline port 5110 >> $LOGFILE1 &
ngrep -t -q -d any -W byline port 5150 >> $LOGFILE2 &
ngrep -t -q -d any -W byline port 5160 >> $LOGFILE3
exec bash
sleep 2
done


Answered By - tink