FTP - notify a user when a file is uploaded?

I have FTP services working on an Xserve running Server 10.5.1 - and the client would like to know if he could get an email notification when someone uploads to it. I'm not sure how I would go about that. anyone?

Xserve Quad-Core 2.8 GHz, Mac OS X (10.5.1)

Posted on Mar 18, 2008 8:53 PM

Reply
22 replies

Apr 21, 2008 1:23 PM in response to Bill Reynolds3

Bill,

You could do it with a shell script and a cron job.
there are a few lines at the beginning that you'll need to edit,
and you'll need a functioning postfix/sendmail on your system,
but other than that it's pretty straightforward.

===== begin shell script (don't copy this line)=====

#!/bin/bash
folderToWatch="/var/ftp"
clientName="John Smith"
clientEmail="jim@smith.com"
fromName="FTP Server"
fromEmail="from@ftp.com"
subjectLine="Files Uploaded!"

files=files.$$
find $folderToWatch/* -newer timestamp -print > $files
if [ -s "$files" ]
then
mail=mail.$$
echo "To: "$clientName >> $mail
echo "Subject: "$subjectLine >> $mail
cat $files >> $mail
/usr/sbin/sendmail -F $fromName -f $fromEmail $clientEmail < $mail
rm $mail
fi
rm $files
touch timestamp

===== end shell script (don't copy this line)=====

save that file as "ftpchecker.sh"

then a line like this in your crontab:
*/10 7-18 * * 1-5 sh /path/to/ftpchecker.sh

(this line tells cron to run the script ever few minutes but
only monday-friday and only between 7am and 6pm)

Hope that helps.
Jeremiah

Apr 21, 2008 2:57 PM in response to Bill Reynolds3

Apple has done it for us as far as generic notification. 1 minute set up required, see below.
Sending an email, looks like a good solution above or possibly someone more familiar can edit the apple script which is in every client by default.

This is very easy as long as the user has access to the folder in question. For the following I assume that if your folder is on a share point you will need to have it mounted. Ours is on a share which each user automounts.

On a client, logged in as the user you desire, make sure you have access to the folder you want notification from.

Open HD > Applications > AppleScript > Folder Actions Setup (this is a script)

In the next window select "enable folder actions"

Hit + under the left side of the window and add the folder you want to note the action in.

Select the folder in the left of the window and hit + under the right side of the window

Choose "add - new item alert"

quit AppleScript.

The user will now be notified anytime something is added to the folder in question with a dialog like "A new item has been added to FOLDER" would you like to see it now" and be given the option to see the new file by responding yes or no to the dialog. If you say yes, you will be taken right to the folder in question and the new item will be selected for you. The dialog is time sensitive and will go away after a short period of time if no response is made.

This item will be retained by the user in their home and not the computer. Each user who wants this feature will need to do this only once. Our users are notified no matter which computer they have logged into.
We use it to notify our users that a Fax has come in via our server's fax.

hope this helps
-Erich

May 6, 2008 4:29 PM in response to Jeremiah Poling

I gave this a whirl and I can see the cronjob running the script every interval, but i haven't gotten an email yet. 🙂 I'll go over the settings again to see if I missed something. As for the Folder Action recommendation, it doesn't work with child folders, so that doesn't help either.

Still trying to get a solution that works. I'm hoping the script will work.

May 6, 2008 5:01 PM in response to Bill Reynolds3

It does require a working sendmail command.
You might try checking the logs to see if sendmail throwing up an error.

or just try sending a message from the commandline to see if it works:
+/usr/sbin/sendmail email@address.com+
you can then type a test message.
a period ( . ) on a line by itself will signal sendmail to close and send the message.

It's entirely possible that I mistyped something in the script.
I do use the script everyday, but in my original, everything is hardcoded. I added the variables to make it easier for you to modify, and I did only one test before I posted it here.

If, after verifying that sendmail is working, you still can't get it going, let me know, and I'll do some more through testing.

May 6, 2008 5:52 PM in response to Bill Reynolds3

Try this:


#!/bin/bash
folderToWatch="/Volumes/Misc/Misc/FTP"
clientName="John Smith"
clientEmail="client@email.com"
fromName="FTP Server"
fromEmail="from@ftp.com"
subjectLine="Files Uploaded!"
files=files.$$
find $folderToWatch/* -newer timestamp -print > $files
if [ -s "$files" ]
then
mail=mail.$$
echo "To: $clientName<$clientEmail>" >> $mail
echo "From: $fromName<$fromEmail>" >> $mail
echo "Subject: $subjectLine" >> $mail
cat $files >> $mail
/usr/sbin/sendmail -t < $mail
rm $mail
fi
rm $files
touch timestamp

May 7, 2008 8:55 PM in response to Jeremiah Poling

I made the appropriate changes per your update, to no avail. I did run a test sendmail message and it delivers immediately, so I know that part is working. I saved the shell script, uploaded a file to the watched directory via FTP, waited... nothing. manually ran the script via: sh ftpchecker.sh ... still nothing. Thoughts? Also, will this recurse subdirectories so if someone uploads into a subfolder, it'll catch that too?

- Bill

May 8, 2008 5:13 AM in response to Bill Reynolds3

this is the line that does the actual work:
find /path/to/folder/* -newer timestamp -print

the "timestamp" is not a code. the end of the script actually creates a file named "timestamp" and after each run of the script, the "touch timestamp" line will change the modification date on the file.
so the find command is looking for files that have a newer modification date than this timestamp file that we've created. if you're placing an older file into the folder you're watching, then it won't show up. (if you have subfolders, place your file into one of them. Then at least the subfolder itself will get listed as having a newer modification date.)
it does recurse.

Do you have any spaces or other odd characters in the path to the folder?
if so, you'll need to escape the spaces with a backslash like this:
folderToWatch="/Volumes/Macintosh HD/Misc/FTP"

It's is case sensitive.

also, make sure that you don't have a trailing slash in the folderToWatch line.

beyond that, I can't think of any reason that it shouldn't work.

You might post just the bits that you customized in the script, so I can look at it.

--Jeremiah

Message was edited by: Jeremiah Poling

May 20, 2008 8:27 AM in response to Bill Reynolds3

Re: over notification. I've gotten used to it, and so I didn't think to mention it.
since it's checking the folder every so many minutes, it will catch half uploaded files. and will continue to catch them every time it runs while the files are in the process of being uploaded.

it's never been a huge issue for us, and I've never taken the time to try and come up with a better solution. If your client doesn't need to know right away, just space the cron job out more, so that files will likely be completely uploaded between runs, and it will help.

Sorry about that.
Jeremiah

May 20, 2008 9:56 AM in response to Bill Reynolds3

An alternative approach... Not exactly what the customer asked for, but one that might well be capable of providing what the customer wants...

(Short questions can lack context, so I'm making assumptions.)

An accessible or semi-open FTP server can be an issue in general (performance, lack of security, frequency of FTP attacks, etc), so I tend to use a web content management system, and allow the upload to occur through that. And since it's a part of the content management system and it's all running php (or Python or whatever your CMS is written in), you can code it to do pretty much anything you want -- if your CMS doesn't already support notifications.

Drupal and Wordpress are two of the better known CMS packages.

Jul 14, 2008 3:27 PM in response to Bill Reynolds3

Questions / comments:

- what output does the script produce? ie: error messages and the like.

- check the listings of all files involved: "files.$$", "mail.$$", the timestamp file, plus the contents of the FTP directory. Look at things like timestamps and filesizes; are they as expected? Also, are the files removed after the script has finished?

- make sure that your current directory of your cronjob is a place where the working files are able to be created. The default from a cronjob is your home directory, and I doubt this is a problem, since you would have noticed straight away. But I'd check anyway.

- I would not include the asterisk on the find command. I'm assuming it's there in order to ensure that "find' goes through all subdirectories. However, that isn't necessary: "find" goes recursively through all contents of the given path anyway. Furthermore, I'd leave it off because then I know exactly what I'm getting on the comand line... perhaps there's another folder or file being found by "find"; you could check this by dumping the contents of "$files" before removing it.

- double-check that your folder to watch does not have any spaces in it. Place double-quotes around the variable in the "find" comand to be sure, ie:

find "$folderToWatch" -newer timestamp -print > $files

- I'd add more tracer statements to that script, using the "||" and "&&" operators. Example:

/usr/bin/sendmail -t < $mail && echo "Mail sent OK"

This echoes the message "Mail sent OK", if the sendmail command did not return an error. Or, inverting the logic to only show a message if there's a problem:

/usr/bin/sendmail -t < $mail || echo "Error sending mail, continuing anyway"

- also try repeating the "find" command, but a) let output go to stdout, and b) add a "-ls" argument to show details of what "find" is finding. See below.

- I'd add another echo statement, to produce a blank line between the mail headers and the mail message body, see below. Sendmail might rely on that blank line to work out where the headers end, and content begins.




The places I'd start with for adding debugging and tracer statements are marked in bold, below:

#!/bin/bash
folderToWatch="/Volumes/Misc/Misc/FTP"
clientName="John Smith"
clientEmail="client@email.com"
fromName="FTP Server"
fromEmail="from@ftp.com"
subjectLine="Files Uploaded!"
files=files.$$
find $folderToWatch -newer timestamp -print > $files *|| echo "Error from find command"*
*find $folderToWatch -newer timestamp -print -ls*
if \[ -s "$files" \]
then
mail=mail.$$
echo "To: $clientName<$clientEmail>" >> $mail
echo "From: $fromName<$fromEmail>" >> $mail
echo "Subject: $subjectLine" >> $mail
*echo "" >> $mail*
cat $files >> $mail
/usr/sbin/sendmail -t < $mail *|| echo "Error from sendmail"*
rm $mail
fi
rm $files
touch timestamp

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

FTP - notify a user when a file is uploaded?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.