Skip to content
Advertisement

Need a way to check content of file (time data) is more than 5 minutes or not

I am creating a script to pull out the list of highest cpu consuming jobs on my server. I am taking the dump of processes in a flat file in below format:

202009080230,4.1,218579,padaemon,02:30,00:00:01,SD_RCC_FRU:wf_rcc_ds_CRS_FactSuspendedGdc_d.s_m_rcc_ds_Staging_Dimension_FactSuspendedGdc_ipi_d

The second data point(4.1) is my cpu utilization while the sixth one is the time taken(00:00:01).

I intend to filter out only the entries from this file that are greater than 75% and run for more than 00:05:00 i.e. 5 minutes. I wrote an if logic for the same like below:

var10=`ls -lrt /projects/Informatica/INFA_ADMIN/LogFiles/myfile.txt |awk '{ print $9 }'`
sed -i 's/ /,/g' $var10
for i in `cat $var10`
do
var12=`echo $i |cut -d',' -f2`
var16=`echo $i |cut -d',' -f6`
if [ $var12 -ge 75.0 ] && ("$var16" > "00:05:00");
then
<logic>
fi
done

the script is able to identify processes taking more than 75.0 cpu but failing in the second condition.

[ 136 -ge 75.0 ] 00:20:26 1> 00:05:00 cpu_report_email_prateek_test.sh[63]: 00:20:26: not found [No such file or directory] Can you please guide how this can be fixed?

Advertisement

Answer

First, I understand that on line 1 you’re trying to have the latest file. I would rather have the file created always with the same name, then at the end of the script, after processing it, mv to another name (like myfile.txt.20200101). So you’ll always know that myfile.txt is the latest one. More efficient than doing ls. Then again, you could improve your line with : (it’s the number one, not the letter “l”)

var10=$(ls -1rt /projects/Informatica/INFA_ADMIN/LogFiles/myfile.txt)

From your line 2, I assume the original file is space delimited. Actually, you might use that to your advantage instead of adding commas. Also, using a for loop with “cat” is less performant than using a while loop. For the time comparison, I would use a little trick. Transforming your threshold and reading, in second since epoch then compared them.

MyTreshold=$(date -d 00:05:00 +"%s")    
while read -r line; do
        MyArray=( $line )
        MyReading=$(date -d ${MyArray[5]} +"%s")
        #the array starts a index 0
        if [[ ${MyArray[1]} -ge 75 && $MyReading -gt $MyThreshold ]]; then
           <LOGIC>
        fi
done <$var10
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement