Execution problem with Cron

Hi,
I'm unable to understand why the grep command in cronjob works intermittently

Expected output:
Grep command to look for a particular string with today's date and exit from infinite while loop else it must sleep for 5 mins

I'm using the following statement in my shell script, the variable $logString is empty even when the pattern exists in the log

while [ -z "$logString" ]
                do
                        echo "Sleep for 5 mins" >> $LOG
                        sleep 300
                        # logString=$(/usr/bin/perl -ne '/STATUS\s+\['"$month"'\/'"$day"'\s+(05|06).*\]\:\s+main\:\s+Active\s+region.*/ && print' << logname>>)
                        logString=$(grep "$(/usr/bin/date +"\%m/\%d")" /ilx/log/exrMngr.log | /usr/bin/grep ".*0[56]\:[0-9]\{2\}\:[0-9]\{2\}" | /usr/bin/grep 'Active region has been')
                        echo "String is: $logString" >> $LOG
                done 

while it was successful in yesterday's cron run

Sleep for 5 mins
String is: STATUS [06/09 05:04:57]: main: Active region has been changed from: 0 to: 1

it failed today

Sleep for 5 mins
String is:

Three things we need to help:

1 What UNIX?
2. What shell?
3. a sample of the log file that you know has the pattern.  A few lines are great.

Otherwise we are just guessing what is going on. Thanks.

1 Like

1 What UNIX?
- SunOS 11.4.5
2. What shell? -
- /bin/sh
3. a sample of the log file that you know has the pattern. A few lines are great.

             - STATUS [06/10 05:05:01]: Total counts of security records: 8982673.
               STATUS [06/10 05:05:02]: main: Active region has been changed from: 1 to: 0
               STATUS [06/10 05:06:02]: elapsed time: 60; vait_time: 1800

Cronjob starts at 5, ideally it should have exit at 05:05:02 when it finds the matching string

Snap from the cronjob log

============ Started at Sun Jun  9 05:00:00 EDT 2019 ==========
Put to maintenance
Sleep for 5 mins
String is: STATUS [06/09 05:04:57]: main: Active region has been changed from: 0 to: 1
Flush_cache.sh Completed
Taking the process out of maintenance

Any suggestions from anyone?

Well, I can't see an error / failure. No "intermittend" behaviour. Data is from 10. June, and log (which seems to have worked) from 9. June. They can't possibly match.

Hey RudiC
thanks for your response

I have managed to fix this issue, now its working consistently
There was no escape sequence needed for %m and %d

Now after fixing the line reads like this

logString=$(/usr/bin/grep "$(/usr/bin/date +"%m/%d")"  <log name> | /usr/bin/grep ".*0[56]\:[0-9]\{2\}\:[0-9]\{2\}" | /usr/bin/grep 'Active region has been')

Why three grep s? Why not

logString=$(/usr/bin/grep  "$(/usr/bin/date +"%m/%d") 0[56]\(:[0-9]\{2\}\)\{2\}.*Active region has been"  <log name>)

Thanks RudiC

i'll try this command and keep you posted :slight_smile:

Hi RudiC

logString=$(/usr/bin/grep  "$(/usr/bin/date +"%m/%d") 0[56]\(:[0-9]\{2\}\)\{2\}.*Active region has been"  <log name>)

does not seem to work, i have reverted my code back to using 3 greps

Thank you for your suggestions

Well, strange, it worked perfectly fine for me. Did you adapt the date command so it reflects the timestamp you're after?