Solved

script to get latest file and email

Posted on 2011-03-09
25
414 Views
Last Modified: 2012-05-11
Hi Experts,

I have a production server ( windows 2008) , there are multiple processes running on that server. If a process fails due to some reason it creates a text file and stores it in a folder called email data.

What i am trying to do here is to write a script that will get the read that latest created txt file from that folder and email it to me.

I have mounted that folder on a ubuntu box.

I have a rough idea on how to get a latest file & read it .


get a latest file - find . -not -type d -printf "%T+ %p\n" | sort -n | tail -1

how to read a file :

FILE="/mnt/emaildata/1.txt"
OUT=$(awk '{ print $2 }' $FILE)
echo "*** error ***"
for ip in $OUT
do
   echo $ip
done


But i am unsure how do i structure the script so that it will only send the latest file created & i want it to run continuously or at least every 30 seconds.

Please help


Thanks
0
Comment
Question by:santoshmotwani
  • 14
  • 11
25 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 35089931
If I am understanding you correctly


You want to read the latest file from a folder

 
while :
do
   folder=/path/to/folder
   file=$(ls -lt $folder | head -1 | awk '{print $1}')

   #email it
   echo "lastest file" | mutt -a $file EMAIL@address
   sleep 30s
done

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 35089949
In awk about it would be I think $9.  Try that.  Sorry about that.  If you don't have mutt, you can use mailx or mail, but that would put the text in your body.
Your command would look like
cat $file | mail -s "SUBJECT" email@address
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35089957
To get the latest file I would also try the following command.

files=$(ls -t)
latest=$(${files[0]})
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35089966
hi farzani ,


thnx for your help mate,,, but i also want to make sure it is based on creation date, as well as if the file is already sent it should not be sent again..


Hope its clear
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35090073
-t option sorts by modification time and that is probably what you need.  So, if the same file is being written to over and over, you need modification time.  And if a new file is being created, still modification time should work
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35090099
lets take out email part.

#!/bin/bash

while :
do
   folder="/mnt/test3/"
   file=$(ls -lt $folder | head -1 | awk '{print $9}')

   #email it
   echo $file
done


this gives me :
galert2.sh: line 5: /tmp/: is a directory
galert2.sh: line 5: /tmp/: is a directory


Please help
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35090115
oh sorry i changed directory to /tmp instead of /mnt/test3
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35090131
Run the following and send me the output


#!/bin/bash

folder="/mnt/test3/"
file=$(ls -lt $folder)
echo FILES ARE: $file

latest_long=$(ls -l | head -1)
echo LONG IS $latest_long

latest=$(ls -l | awk '{print $9}')
echo $latest
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35090147
The above one is the troubleshooting script to see what is going on
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35090168
:17 2009-10-12 drwxrwxrwx 2 root staff 0 2010-09-28 17:11 2009-10-09 drwxrwxrwx 2 root staff 0 2010-09-28 17:10 2009-10-08 drwxrwxrwx 2 root staff 0 2010-09-28 17:08 2009-10-07 drwxrwxrwx 2 root staff 0 2010-09-28 17:06 2009-10-06 drwxrwxrwx 2 root staff 0 2010-09-28 17:01 2009-10-05 drwxrwxrwx 2 root staff 0 2010-09-28 17:00 2009-10-02 drwxrwxrwx 2 root staff 0 2010-09-28 16:59 2009-10-01 drwxrwxrwx 2 root staff 0 2010-09-28 16:56 2009-09-30 drwxrwxrwx 2 root staff 0 2010-09-28 16:53 2009-09-29 drwxrwxrwx 2 root staff 0 2010-09-28 16:52 2009-09-28 drwxrwxrwx 2 root staff 0 2010-09-28 16:47 2009-09-25 drwxrwxrwx 2 root staff 0 2010-09-28 16:47 2009-09-26 drwxrwxrwx 2 root staff 0 2010-09-28 16:45 2009-09-24 drwxrwxrwx 2 root staff 0 2010-09-28 16:43 2009-09-23 drwxrwxrwx 2 root staff 0 2010-09-28 16:41 2009-09-22 drwxrwxrwx 2 root staff 0 2010-09-28 16:40 2009-09-21 drwxrwxrwx 2 root staff 0 2010-09-28 16:35 2009-09-18 drwxrwxrwx 2 root staff 0 2010-09-28 16:33 2009-09-17 drwxrwxrwx 2 root staff 0 2010-09-28 16:31 2009-09-16 drwxrwxrwx 2 root staff 0 2010-09-28 16:29 2009-09-15 drwxrwxrwx 2 root staff 0 2010-09-28 16:28 2009-09-14 drwxrwxrwx 2 root staff 0 2010-09-28 16:22 2009-09-11 drwxrwxrwx 3 root staff 0 2010-09-28 16:20 2009-09-10 drwxrwxrwx 2 root staff 0 2010-09-28 16:18 2009-09-09 drwxrwxrwx 2 root staff 0 2010-09-28 16:17 2009-09-08 drwxrwxrwx 2 root staff 0 2010-09-28 16:16 2009-09-07 drwxrwxrwx 2 root staff 0 2010-09-28 16:10 2009-09-04 drwxrwxrwx 2 root staff 0 2010-09-28 16:08 2009-09-03 drwxrwxrwx 2 root staff 0 2010-09-28 16:06 2009-09-02 drwxrwxrwx 2 root staff 0 2010-09-28 16:05 2009-09-01 drwxrwxrwx 2 root staff 0 2010-09-28 16:04 2009-08-31 drwxrwxrwx 2 root staff 0 2010-09-28 15:58 2009-08-28 drwxrwxrwx 2 root staff 0 2010-09-28 15:56 2009-08-27 drwxrwxrwx 2 root staff 0 2010-09-28 15:55 2009-08-26 drwxrwxrwx 2 root staff 0 2010-09-28 15:52 2009-08-25 drwxrwxrwx 2 root staff 0 2010-09-28 15:48 2009-08-24 drwxrwxrwx 2 root staff 0 2010-09-28 15:47 2009-08-21 drwxrwxrwx 2 root staff 0 2010-09-28 15:45 2009-08-20 drwxrwxrwx 2 root staff 0 2010-09-28 15:43 2009-08-19 drwxrwxrwx 2 root staff 0 2010-09-28 15:41 2009-08-18 drwxrwxrwx 2 root staff 0 2010-09-28 15:40 2009-08-17 drwxrwxrwx 2 root staff 0 2010-09-28 15:34 2009-08-14 drwxrwxrwx 2 root staff 0 2010-09-28 15:33 2009-08-13 drwxrwxrwx 2 root staff 0 2010-09-28 15:31 2009-08-12 drwxrwxrwx 2 root staff 0 2010-09-28 15:29 2009-08-11 drwxrwxrwx 3 root staff 0 2010-09-28 15:28 2009-08-10 drwxrwxrwx 2 root staff 0 2010-09-28 15:23 2009-08-07 drwxrwxrwx 2 root staff 0 2010-09-28 15:21 17052010 drwxrwxrwx 2 root staff 0 2010-09-28 15:19 10052010 drwx------ 2 1114 1029 0 2008-08-08 12:48 lost+found
LONG IS total 6276
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35090290
Ok, run this please:

#!/bin/bash

folder="/mnt/test3/"
latest_long=$(ls -lt $folder | find -type f | head -1)

echo LATEST LONG: $latest_long
echo

latest=$(ls -l | awk '{print $9}')
echo LATEST:  $latest
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35090310
LATEST LONG: ./var/lock/evms-engine

LATEST: -> -> -> -> ->
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 31

Expert Comment

by:farzanj
ID: 35090312
Sorry replace the last two lines

latest=$(echo $latest_long | awk '{print $9}')
echo "LATEST : $latest"
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35090393
LATEST LONG: ./var/lock/evms-engine
LATEST :
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35090437
Sorry one more, unfortunately, I don't have a terminal myself


#!/bin/bash

folder="/mnt/test3/"
latest_long=$(ls -lt $folder | grep -v "^d" | head -1)

echo LATEST LONG: $latest_long
echo

latest=$(echo $latest_long | awk '{print $8}')
echo "LATEST : $latest"
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35090454
LATEST LONG: total 928

LATEST :
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35090457
One last

#!/bin/bash

folder="/mnt/test3/"
latest_long=$(ls -lrt $folder | grep -v "^d" | tail -1)

echo LATEST LONG: $latest_long
echo

latest=$(echo $latest_long | awk '{print $8}')
echo "LATEST : $latest"
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35090568
LATEST LONG: -rwxrw-rw- 1 1070 root 0 2011-03-10 13:35 New Text Document.txt

LATEST : New


some success now all we want to see whts in tht file .... appreciate your help
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35090582
You know what, just do awk '{print $NF}'

So change the second last line to

latest=$(echo $latest_long | awk '{print $NF}')


Sorry, was too tired and don't even have a terminal in front of me
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35093758
still unable to display file contents
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35094182
Hi, it is morning and I can think now.
I thought you would assemble it now.  But it is alright.  Here it goes.

If you want to display the contents of the file once, here is the code.
 
cat $(ls -Ft | grep -v '/' | head -1)

Open in new window


If however you want to send it via email, in the body and only once, then:
 
cat $(ls -Ft | grep -v '/' | head -1) | mail -s "Latest file" email_address@domain.com

Open in new window


If however, you want to repeat this process every 30 seconds, this is what you would write
while :
do
   cat $(ls -Ft | grep -v '/' | head -1) | mail -s "Latest file" Email_Address@domain.com
   #wait for 30 seconds
   sleep 30s
done

Open in new window


I hope this is what you wanted.
Cheers
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35103496
Hi,

good stuff only one issue.... there are all.txt files in there.... and i get this error when i run this script.

cat: 5660.TXT*: No such file or directory

Any input?
0
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 35104297
Try this:
 
cat $(ls -lrt | grep -v "^d" | tail -1 | awk '{print $NF}')

Open in new window


For email do this:
 
cat $(ls -lrt | grep -v "^d" | tail -1 | awk '{print $NF}')| mail -s "Latest file" email_address@domain.com

Open in new window

0
 
LVL 16

Author Closing Comment

by:santoshmotwani
ID: 35104456
Sorry mate for being a such a pain... but u did a gr8 job ......
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35104560
Glad to help
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now