LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-05-2012, 03:14 PM   #1
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Rep: Reputation: Disabled
Hot folder on network


So Ive been really enjoying and hating at the same time learning bash this past couple of weeks. Its a joy to be able to get something to work in the terminal but I wanted to know can a script be created that could check a folder on a network?? I would like to be able to drop a file in a folder and it would automatically process the files. Any ideas or direction would be great.
 
Old 11-05-2012, 11:40 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
inotifywait is probably what you're looking for. Never used it myself, so can't be of much assistance further.
 
1 members found this post helpful.
Old 11-06-2012, 12:14 PM   #3
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Wim Sturkenboom View Post
inotifywait is probably what you're looking for. Never used it myself, so can't be of much assistance further.
thanks I will look into it
 
Old 11-09-2012, 01:48 PM   #4
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
I still dont see how this would help if I drop a file into a folder and the script will run once a file is place into it.
 
Old 11-09-2012, 02:18 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 12,058

Rep: Reputation: 759Reputation: 759Reputation: 759Reputation: 759Reputation: 759Reputation: 759Reputation: 759
Is this network folder on a windows, linux computer or some other device? There is more the one way to parse the strings but below is one method. When a file is written to /folder (for example)it generates an event of which the output includes the filenane.

Code:
#!/bin/bash
/usr/bin/inotifywait -e close_write -mrq /folder | while read line; do
set -- "$line"
IFS=" "; declare -a Array=($*)
FILE="${Array[2]}"
  Your code goes here
fi
done
http://en.wikipedia.org/wiki/Inotify
http://linux.die.net/man/1/inotifywait
 
Old 11-09-2012, 02:42 PM   #6
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,572
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by graphicsmanx1 View Post
...the script will run once a file is place into it.
No, no, no, inotify via the script is running before the file is dropped into place.

http://www.ibm.com/developerworks/li...ify/index.html
 
1 members found this post helpful.
Old 11-13-2012, 11:10 AM   #7
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
My goal is to place this on a server and once a file is uploaded to a folder on the network the bash script will run. inotify appears to work only IF the script is already running. The server will be at an idle state and I want the file to be a trigger for the script till all the files are ran through
 
Old 11-13-2012, 11:43 AM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by graphicsmanx1 View Post
inotify appears to work only IF the script is already running.
The solution, then is to start the script some time before the files are dropped into the directory. Do you want the script to run on the server host, or on the client host? If the former, then you can start the inotify script on the server, perhaps at boot time. Otherwise, you will have to start it on the client(s) at some opportune time.
There is no property or behavior that can be imposed on a filesystem generally, that will launch a process. How soon after the file is written does the processing need to occur? Would it be sufficient to use a 1-minute cron job to process some or all files in a specific directory, or list of directories?

--- rod.
 
Old 11-13-2012, 11:44 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 12,058

Rep: Reputation: 759Reputation: 759Reputation: 759Reputation: 759Reputation: 759Reputation: 759Reputation: 759
Yes, as already stated the script is idle until inotifywait detects a file being written to the designated folder. You can modify the script so your process is run when a particular file name is written to the folder.
 
Old 11-13-2012, 12:17 PM   #10
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr View Post
The solution, then is to start the script some time before the files are dropped into the directory. Do you want the script to run on the server host, or on the client host? If the former, then you can start the inotify script on the server, perhaps at boot time. Otherwise, you will have to start it on the client(s) at some opportune time.
There is no property or behavior that can be imposed on a filesystem generally, that will launch a process. How soon after the file is written does the processing need to occur? Would it be sufficient to use a 1-minute cron job to process some or all files in a specific directory, or list of directories?

--- rod.

Im getting a few blades from a friend and I want to be able to test sizes and sort files based on some bash I wrote. I was trying to create a network hot folder that the files will be dropped to, do their testing and sort the files. I was wanting to write a script that will check the folder every 5 mins then sleep. I guess I will look into a "cron" job. Still new and eager to learn.
 
Old 11-13-2012, 12:30 PM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
I guess I will look into a "cron" job.
Okay, then just to clarify: cron is a process that launches arbitrary other processes on a scheduled basis, which you configure. It's scheduling granularity is one minute. cron itself doesn't process anything; it just schedules and launches your processes.

--- rod.
 
1 members found this post helpful.
Old 11-13-2012, 01:33 PM   #12
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
As stated, I've never used inotify.

Have you had a look at example 2 in the man page? Modify to your needs and start the script when your system boots.
Code:
#!/bin/bash
while inotifywait -e close_write logdir; do
    ls logdir
done
Replace ls logdir by something more useful (possibly calling the other script that you want to be executed). Test by making it executable and starting it in a terminal; use an other terminal to drop files in logdir and see what happens. Once it's modified to your needs (have a look at michaelk's earlier code for some optimizations), you can start the script at boot.

Note:
it will run in an endless loop.

PS
the output of above script
Code:
wim@i3-2120:~/inotifywait_test$ ./test.sh 
starting loop
Setting up watches.  
Watches established.
logdir/ CLOSE_WRITE,CLOSE myfile.2.txt
myfile.2.txt  myfile.txt
Setting up watches.  
Watches established.
logdir/ CLOSE_WRITE,CLOSE myfile.1.txt
myfile.1.txt  myfile.2.txt  myfile.txt
Setting up watches.  
Watches established.
logdir/ CLOSE_WRITE,CLOSE myfile.0.txt
myfile.0.txt  myfile.1.txt  myfile.2.txt  myfile.txt
Setting up watches.  
Watches established.
^C

Last edited by Wim Sturkenboom; 11-13-2012 at 01:45 PM.
 
Old 11-13-2012, 05:04 PM   #13
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
this seems like a great solution I might proceed with: here and here


good read here too

Last edited by graphicsmanx1; 11-13-2012 at 05:18 PM.
 
Old 11-13-2012, 11:49 PM   #14
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
At least you did your research It still requires something (cron) to start your script, so there is not much difference with the other solutions with inotifywait

Good luck.
 
Old 11-14-2012, 09:23 AM   #15
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
oh I will test each one and ask accordingly. I do enjoy coding overall but this is a new learning step for me
 
  


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
[SOLVED] network using linux mint 13 and verizon wireless broadband hot spot tbrownarcher Linux Mint 8 08-25-2012 09:45 PM
[SOLVED] wi-fi hot spot excludes mobile network on Android vincix Linux - Mobile 1 11-24-2011 09:12 PM
How to set up public folder/ share folder for my network? Kiwi89 Linux - Server 6 10-31-2011 06:10 AM
network folder through ssh jani_fedora Linux - Networking 1 03-26-2008 04:50 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration