sh script for file received confirmation

I am new at scripting, (and this fourm- so hi guys!) and I was wondering is there something out there that I can use to do the following:

I receive a file on an FTP server
I would like a script to look at a specifc directory for a certain file
and when it arrives it will send an email confirmation.

the only caveat i see is that the same file sits there in the directory
until a new one overwrites it each day. so there will always be a file there.

I already have sendmail running so that is no issue

anything out there that I can modify?

please help!

:slight_smile:

Thanks..

In sh: Tweak F to your needs. A bit quick but it should work

#!/bin/sh

F='/path/to/that/certain/file'
MD5='/tmp/md5ofthatcertainfile'

[ "$(md5sum "$F")" = "$(cat "$MD5" 2>/dev/null)" ] || {
	md5sum "$F" > "$MD5"
	# Send your e-mail here
}

exit 0

Schedule it with cron ( crontab -e typically)

1 Like

Crontab this script before time of earliest arrival:

#!/usr/bin/ksh
 
touch /tmp/fmind.mark
 
while [ 1 ]
do
  find your_file -newer /tmp/fmind.mark | read f
  if [ "$f" != "" ]
  then
   break
  fi
  sleep 120
done
 
ls -l $f | mailx -s "New $f" you@yours.com
1 Like

Thanks again to both of you!
I played with them both and this is what I can up with.
Seem to work when i run it as a cron every hour, if the file has been changed it will send the email.

#!/bin/sh
F='/home/test'
MD5='/tmp/md5ofthatcertainfile'
[ "$(md5sum "$F")" = "$(cat "$MD5" 2>/dev/null)" ] || {
        md5sum "$F" > "$MD5"
SUBJECT="test file"
EMAIL=myemail@email.com
EMAILMESSAGE="test file"
echo "test file received successfully"> $EMAILMESSAGE
echo "This is an automated message please do not reply" >>$EMAILMESSAGE
# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
}
exit 0

would you guys have done anything differently?

I am new at this, so it may look a bit ugly.. haha

I had one other question

How can I incorporate in the email, the date,time stamp of the file?
that would be the last piece :slight_smile:

again this is a big help.. thanks...

One caveat is that the file might not be wholly received to set the modify time or change checksum or hash. Nice file transmitters have some way to signal a good transfer, like an ack file or name change. At least, before using the file, use fuser to ensure it is not still being written.

1 Like

So could I use fuser to tell me if the file is still being transmitted vs completed?

If you don't mind a bit of delay, there's a much simpler way: Check if the file's been modified in the last 10 minutes. This won't tell you if the connection broke and left an incomplete file, but neither will fuser.

1 Like

As other posters imply the sending process needs improvement. There are three common approaches:

1) Transmit each file under a temporary filename, then the transmission process renames each file after the transfer is complete. This is not 100% safe because whole files could be missing.
Also verify that each file is dated today.
2) Transmit each file under a temporary filename, then the transmission process renames each file and then finally transmits an "end of transmission" file to signify the end of the transmission.
Also verify that each file is dated today.
3) Construct a batch of files and send a "start of transmission" file and and "end of transmission file" containing control records for the batch containg a list of files and their respective checksums.
Also verify that each file is dated today.

I use a combination of 2) and 3) with an automatic retry mechanism.

The "fuser" approach is flawed because it will ignore a failed or missing transmission. I can't think of any reliable way of determining whether an incoming ftp has finished just by looking at the incoming file.

Thanks again everyone.

How can I incorporate in the script I am using to include in the email that I send the date,time stamp, and size of the file?