As chrism01 has stated, I agree that you should be using inotifywait to monitor the directory rather than doing a minute cron job like my original scripting post. I've modified my original script do that. It will now also email you.
You'll need to install the inotify-tools package.
Let's call this script: /usr/local/bin/monitor_and_move_files.sh
Code:
#!/bin/bash
#one ore more emails separated by as space character
EMAIL_ADDRESSES="your.email@server.com"
SUBJECT="Files have been uploaded"
UPLOAD_DIRECTORY="/home/mp/public_html/aoe2hdozclan.com/ftp/upload"
function email_me() {
#email a list of files uploaded
mail -s "${SUBJECT}" ${EMAIL_ADDRESSES} <<EOF
Here's a list of files that have been uploaded...
$(ls -1 "${UPLOAD_DIRECTORY}")
EOF
}
cd "${UPLOAD_DIRECTORY}"
while inotifywait -e modify "${UPLOAD_DIRECTORY}";do
#email me the list of files
email_me
#move them all one directory up.
mv --backup=numbered * ../
#now loop back around and monitor the UPLOAD_DIRECTORY again for uploaded files.
done
If you get tired of getting emails because of high server usage you can comment out the "email_me" command from within the while loop.
You can execute this script to continue running after you log out in one of two ways. The nohup command or a subshell from root.
Code:
#nohup example
nohup /usr/local/bin/monitor_and_move_files.sh > /dev/null &
#subshell as root example
su - -c "/usr/local/bin/monitor_and_move_files.sh > /dev/null" &
#subshell as a user other than root example (using user name "someuser")
su - someuser -c "/usr/local/bin/monitor_and_move_files.sh > /dev/null" &
It is not smart to run random scripts from a forum without at least understanding what is going on. Search for the following terms in the
bash man page.
- Here Documents - It is what is being used with the mail command (<<EOF).
- Command Substitution - That's being used by $(ls -1) to simply execute the ls command.
- Compound Commands - It is what is being used by the "if [ stuff ];then" command. If you would like to learn more about the [ command (/bin/[) aka test see the test man page. Remember, bash uses program exit codes to process logic.
- Shell Function Definitions - The function listed in my script.
- Parameters - This will explain bash variables. See also Parameter Expansion for more advanced usage.
To get the inotifywait command you'll need to install the inotify-tools package.
Warning: My script is completely untested and I've not used the inotifywait command before. I built the command from the man page. That being said you should thoroughly test it because I don't know if the modify event will prematurely move the file before it is finished uploading or what. If that's the case you may want to use the close_write event. See the inotifywait man page for details.