LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-26-2013, 02:56 AM   #16
mdexter
LQ Newbie
 
Registered: Apr 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled

I hsve just realised this is not possible but what about myself being alerted by an email to me advising of a new file that has been uploaded once it hits the ftp directory. Just a thought many thanks
 
Old 04-26-2013, 03:11 AM   #17
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
You can put http://linux.die.net/man/1/inotifywait in a script eg Example 2, so it could email you, but you'd flooded if its a busy site... your choice
 
Old 04-26-2013, 03:35 AM   #18
mdexter
LQ Newbie
 
Registered: Apr 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
Many thanks chrism01.

I am a pure newbie here I would have no idea where to start. listen that is ok I will not bother with the email event, I will just go and check the folder periodically, the script makes sense but adding it to mine I would have not a clue, many thanks anyway, I really appreciate your help

regards
 
Old 04-26-2013, 10:20 AM   #19
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,831
Blog Entries: 36

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
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.

Last edited by sag47; 04-26-2013 at 09:36 PM.
 
Old 04-26-2013, 10:42 AM   #20
mdexter
LQ Newbie
 
Registered: Apr 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
absolutely awesome.......ok please see image, this is the files etc in the inotify folder, do I simply copy the contents of this folder to a folder in my webspace, for example:

/home/mp/public_html/aoe2hdozclan.com/inotify/

now how does your new script see these inotify files to be able to work

I see you have the cron job for:

/usr/local/bin/monitor_and_move_files.sh

so if I understand you correctly the inotify directory from the image i have shown you the:

monitor_and_move_files.sh

needs to be in the inotify folder??

is this where it needs to be to make the inotify files work, sorry I hope this makes sense??
Attached Thumbnails
Click image for larger version

Name:	inotify.jpg
Views:	15
Size:	23.2 KB
ID:	12406  
 
Old 04-26-2013, 11:01 AM   #21
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,831
Blog Entries: 36

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
You should install the inotify-tools package using the package management software in your distro. What distro are you using?

Also I'm not using any cron job in my last example. You should not be using cron at all since we're using inotify.
 
Old 04-26-2013, 05:55 PM   #22
mdexter
LQ Newbie
 
Registered: Apr 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
ok, so do i replace a name of one of the current file in the inotify to: monitor_and_move_files.sh

and do I install inotify to /usr/local/bin/ ?

This was the response below my IT web hosting people told me:

____________________

Is there a files that you need to upload to install it? If there is, you can just upload it to your File Manager and do the installation process. If it need to have shell access, I am sorry but it's not possible.

______________________

am i still ok to do this???

Thank guys
 
Old 04-26-2013, 07:23 PM   #23
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
The inotify tools will be in the repos for your distro (which you still need to tell us).
If you're running only via GUI, there should be an option in the menus to call the installer and ask for that pkg.
Highly recommend you start using the cmd line.

That image shows an install from src, which is not a good idea (its fiddly) and using the repo is much easier.
 
Old 04-26-2013, 08:47 PM   #24
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,831
Blog Entries: 36

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
You should request from your hosting provider that the inotify-tools package be installed if they don't allow you to have shell access. You're not being very clear about how much control you have over this system. Up to this point I've assumed you had some kind of shell access since you're modifying cron jobs and executing scripts. It's still possible to use the inotify script with a cron job to initially start it. You'll have to add an extra section at the beginning of the script to check if the script is already running (and not start so you don't have duplicates). That's the only other way I can think of without using the original minute by minute model.

Last edited by sag47; 04-26-2013 at 08:51 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux cron job duplicate job question cpthk Linux - Newbie 4 09-11-2009 09:52 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 09:16 AM
Cron job in Linux kulan Linux - Software 3 12-18-2006 02:25 PM
Cron job in Suse Linux excidy Linux - Software 8 06-12-2006 11:32 PM


All times are GMT -5. The time now is 04:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration