Script to email status

Hi,

I have few processes in the server continuous run few jobs, each of the process will generate a log file which detailing when its jobs are completed.
the logfile will has the name something like this, result1.log, result2.log, result3.log,.... result10.log, result11.log, result12.log.......
I want to grep all the logfiles with the keyword "completed", the line of the "completed" keyword will also has the date/time info, this will shows me when was the jobs are completed, also I want to sort the output based on logfile name in order, e.g. result1.log, result2.log, result3.log, result4.log etc... and then send me an email.
Anyone help me to construct the script will great appreciate.

Because your filenames don't readily sort in order (as result2.txt appears to come after result10.txt but should come before it) you'll need to either teach the sort command to understand your name or explicitly list them all in the right order.

Once you have that all in some variable (say $FILES for example), you just need to grep them and pipe the output into mail.

eg

for file in $FILES
do
  grep "completed" $file
done | mail -s 'All these things are completed!' me@myemailaddress.com

You could run grep on the list directly, but then you've got to suppress grep's default behaviour of listing the filename before each output line.

Hi,
thanks for the coding.
I have below files in my server:

output1.log
output2.log
output3.log
output4.log
output5.log
output6.log
output7.log
output8.log
output9.log
output10.log
output11.log
output12.log

when I do ls -1 | sort it will comes out as below:

output1.log
output10.log
output11.log
output12.log
output2.log
output3.log
output4.log
output5.log
output6.log
output7.log
output8.log
output9.log

The output10.log, output11.log, output12.log are comes out after output1.log, how could I sort the logfiles in the order where output10.log, output11.log and output12.log etc are come out after output9.log?

Thanks

I think you are talking about this sequence.

 ls -1v output*

Hi,
I don't have the v option, ls -lv gave me error
any other alternative?

Please tell me your detail about your operating system.

Hi,
Im using hpux 11.31

Try

ls | sort -k1.7n 
2 Likes

That's excellent RudiC! I've never been able to get that to work before, I have saved that commandline away for future reference. THANKS!