Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

script to get latest file and email

Posted on 2011-03-09
25
Medium Priority
?
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

618 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