Solved

script to get latest file and email

Posted on 2011-03-09
25
422 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

730 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