The following shows you how to run a script of your choice whenever a new file is uploaded.
- Enable detailed FTP logging. Edit the file
/System/Library/LaunchDaemons/ftp.plist
, and add another ‘-l
‘ option to the FTP daemon (yes, there will be two):...
<key>ProgramArguments</key>
<array>
<string>ftpd</string>
<string>-l</string>
<string>-l</string>
<string>-n</string>
</array>
... - Start FTP sharing. Open ‘System Preferences‘, click on ‘Sharing‘, then select ‘File Sharing‘ from the list. Check the ‘on‘ option, then click the ‘Options…‘ button and check ‘Share files and folders using ftp‘.
- At this point you should be able to open a FTP session, upload a file, and have everything logged. Have a look in
/var/log/ftp.log
and make sure everything is logged. You should see something like:...
Jan 5 06:27:49 gandalf ftpd[23214]: ANONYMOUS FTP LOGIN FROM ::1, blah (class: guest, type: GUEST)
Jan 5 06:27:54 gandalf ftpd[23214]: put /test.txt = 6 bytes in 0.007 seconds
Jan 5 06:27:56 gandalf ftpd[23214]: Data traffic: 6 bytes in 1 file
Jan 5 06:27:56 gandalf ftpd[23214]: Total traffic: 743 bytes in 1 transfer
... - Now, let’s write a little bash script that will read the FTPD log, and will act when a new upload arrives. Save the following as a
run_on_upload.sh
, and customize with your own code:
#!/bin/bash
tail -n 0 -f /var/log/ftp.log |
grep --line-buffered --only-matching "put /.*=" |
while read put file delimiter; do
# do something useful
echo ${file:1}
done
- Run the script, upload something, and see that it works… 🙂