LinuxQuestions.org
Review your favorite Linux distribution.
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, 01: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, 02:11 AM   #17
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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, 02: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, 09:20 AM   #19
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
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 08:36 PM.
 
Old 04-26-2013, 09: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:	25
Size:	23.2 KB
ID:	12406  
 
Old 04-26-2013, 10:01 AM   #21
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
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, 04: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, 06:23 PM   #23
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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, 07:47 PM   #24
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
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 07: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 08:52 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
Cron job in Linux kulan Linux - Software 3 12-18-2006 01:25 PM
Cron job in Suse Linux excidy Linux - Software 8 06-12-2006 10:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:58 AM.

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
Open Source Consulting | Domain Registration