3

Full disclosure: We do not have full access to the autosys database, so our only option for keeping history is to create an Oracle and use the autorep command to output data. We have Linux servers using Bash 3.2.

I have created the following script which pulls the job status details. I want to only output to file the details from jobs that failed today. I have tried many different ways of comparing dates as-is and finally settled on converting the date to seconds then comparing today's seconds to the autorep seconds. The script runs, but outputs all jobs in FA and TE status, instead of just the jobs that failed today. What am I doing wrong?

#!/bin/bash

# Get all job start/end/status for current failures/terms

TODAY=$(date '+%D')
TODAYSEC=$(date -d"${TODAY} +%s)

autorep -J SV*_* | egrep -v "gpdata" | grep "GP" | grep grep 'FA\|TE' | while read LINE; do
    JOBNAME=$(echo $LINE | awk '{print $1}')
    LASTSTARTDATE=$(echo $LINE | awk '{print $2}')
    LASTSTARTTIME=$(echo $LINE | awk '{print $3}')
    LASTENDDATE=$(echo $LINE | awk '{print $4}')
    COMPDATE=$(date -d"${LASTENDDATE}" '+%s')
    LASTENDTIME=$(echo $LINE | awk '{print $5}')
    STATUS=$(echo $LINE | awk '{print $6}')
    RUN=$(echo $LINE | awk '{print $7}')
    if (($COMPDATE -eq $TODAYSEC)); then
        echo "$JOBNAME,$LASTSTARTDATE,$LASTSTARTTIME,$LASTENDDATE,$LASTENDTIME,$STATUS,$RUN"
    fi

done >> output_fe_ta.txt
  • I'm not quite sure what you're doing, but I would change the "if" statement to: if [ $COMPDATE -eq $TODAYSEC ]; then to start... – Jeff Schaller Oct 7 '15 at 19:00
  • The script "outputs all jobs in FA and TE status, instead of just the jobs that failed today" -- isn't that what you're telling it to do with your grep calls? (I assume the grep grep 'FA\|TE' is a typo). How do you determine "jobas that failed today"? – glenn jackman Oct 7 '15 at 19:19
  • The double "grep" is a typo. – MKowalski Oct 8 '15 at 12:37
1

A little tidier. I implemented all the grep calls with bash comparisons:

today=$(date -d 00:00:00 +%s)

autorep -J SV*_* | 
while IFS= read -r line; do
    if  [[ $line != *gpdata* ]] &&
        [[ $line == *GP* ]] && 
        { [[ $line == *FA* ]] || [[ $line == *TE* ]]; }
    then
        read jobname laststartdate laststarttime lastenddate lastendtime status run <<<"$line"
        compdate=$(date -d "$lastenddate" +%s)
        if (( compdate == today )); then
            echo "$jobname,$laststartdate,$laststarttime,$lastenddate,$lastendtime,$status,$run"
        fi
    fi
done >> output_fe_ta.txt

In an arithmetic expression, use == not -eq, and the $ are optional.

Avoid ALLCAPSVARNAMES -- one day you'll accidentally use PATH and then wonder why your script is broken.

  • compdate=$(date -d "$lastenddate}" +%s) should be compdate=$(date -d "${lastenddate}" +%s). Your code outputs exactly the same thing mine does. I get a list of all jobs that are in FA or TE status, instead of just the ones that failed or terminated on today's date. – MKowalski Oct 8 '15 at 13:06
  • show some of your input data. I've got nothing to test with. – glenn jackman Oct 8 '15 at 13:16
  • While I was redacting some sample data for you I realized where I had gone wrong. My code now works as I expected. I changed the "today" variable to match yours. I will post the code shortly, once I redact it. – MKowalski Oct 8 '15 at 13:31
0

A change to the TODAY variable works. Thanks to Glenn for this - I believe this is what I was doing wrong, as debugging showed that my original code was not comparing the seconds as I had expected. Also, I found that comparing YYYYMMDD works and I suspect is probably more efficient.

#!/bin/bash

TODAY=$(date 00:00:00 +%s)
# TODAY=$(date '+%Y%m%d')

autorep -J GP*_* | egrep -v "gpdata" | grep "GP" | grep grep 'FA\|TE' | while read LINE; do
    JOBNAME=$(echo $LINE | awk '{print $1}')
    LASTSTARTDATE=$(echo $LINE | awk '{print $2}')
    LASTSTARTTIME=$(echo $LINE | awk '{print $3}')
    LASTENDDATE=$(echo $LINE | awk '{print $4}')
    COMPDATE=$(date -d"${LASTENDDATE}" '+%s')
    # COMPDATE=$(date -d"${LASTENDDATE}" '+%Y%m%d')
    LASTENDTIME=$(echo $LINE | awk '{print $5}')
    STATUS=$(echo $LINE | awk '{print $6}')
    RUN=$(echo $LINE | awk '{print $7}')
    if (( COMPDATE = $TODAY )); then
        echo "$JOBNAME,$LASTSTARTDATE,$LASTSTARTTIME,$LASTENDDATE,$LASTENDTIME,$STATUS,$RUN"
    fi

done >> output_fe_ta.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.