• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

script to get latest file and email

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
santoshmotwani
Asked:
santoshmotwani
  • 14
  • 11
1 Solution
 
farzanjCommented:
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
 
farzanjCommented:
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
 
farzanjCommented:
To get the latest file I would also try the following command.

files=$(ls -t)
latest=$(${files[0]})
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
santoshmotwaniAuthor Commented:
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
 
farzanjCommented:
-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
 
santoshmotwaniAuthor Commented:
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
 
santoshmotwaniAuthor Commented:
oh sorry i changed directory to /tmp instead of /mnt/test3
0
 
farzanjCommented:
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
 
farzanjCommented:
The above one is the troubleshooting script to see what is going on
0
 
santoshmotwaniAuthor Commented:
: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
 
farzanjCommented:
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
 
santoshmotwaniAuthor Commented:
LATEST LONG: ./var/lock/evms-engine

LATEST: -> -> -> -> ->
0
 
farzanjCommented:
Sorry replace the last two lines

latest=$(echo $latest_long | awk '{print $9}')
echo "LATEST : $latest"
0
 
santoshmotwaniAuthor Commented:
LATEST LONG: ./var/lock/evms-engine
LATEST :
0
 
farzanjCommented:
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
 
santoshmotwaniAuthor Commented:
LATEST LONG: total 928

LATEST :
0
 
farzanjCommented:
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
 
santoshmotwaniAuthor Commented:
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
 
farzanjCommented:
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
 
santoshmotwaniAuthor Commented:
still unable to display file contents
0
 
farzanjCommented:
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
 
santoshmotwaniAuthor Commented:
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
 
farzanjCommented:
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
 
santoshmotwaniAuthor Commented:
Sorry mate for being a such a pain... but u did a gr8 job ......
0
 
farzanjCommented:
Glad to help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 14
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now