LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS
User Name
Password
CentOS This forum is for the discussion of CentOS Linux. Note: This forum does not have any official participation.

Notices


Reply
  Search this Thread
Old 08-25-2017, 03:43 PM   #31
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled

i may be being thick here sorry but i still dont get it

so THEFOLDER=1 so in your script you just type the variable $THEFOLDER but then shouldnt 1 be the actual folder path ie /mnt/invoicetest because whereand how does it know what 1 is

and THEFILE shouldnt it be * ie wildcard/any so THEFILE=*

Last edited by robertkwild; 08-25-2017 at 04:14 PM.
 
Old 08-26-2017, 04:04 AM   #32
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,483

Rep: Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556
Ok, this is the quick version.....
  • When you run a bash script anything you put after the script name get passed as "positional parameters".
  • These start at 1 through whatever....
  • So if you run the script /home/myscript.sh The Quick Brown Fox in your script the variable $1 will have the value The, $2 will be Quick, $3 = Brown, $4 = Fox
When we set up the incrontab line:
Code:
/home/incron/source IN_DELETE,IN_CLOSE_WRITE /home/incron/action.sh $@ $# $% /home/incron/dest
we are telling incrond that when the line "fires" for a file to decode the mystical symbols $@ $# $% and then run the script /home/incron/action.sh with them. So if you create a file testfile1 incrond will decode the symbols as follows:
Code:
$@ /home/incron/source
$# testfile1
$% IN_CLOSE_WRITE
and then will run the script as though you'd typed this in the command line:

Code:
/home/incron/action.sh /home/incron/source testfile IN_CLOSE_WRITE /home/incron/dest
Which means that in the script the positional parameters will be:

Code:
$1 /home/incron/source
$2 testfile1
$3 IN_CLOSE_WRITE
$4 /home/incron/dest
Personally I hate having to remember what parameter is what in a script and also going back and looking at a line and trying to work out what $3 is way down a file, so it's my personal scripting preference to declare and populate more meaningful variables from the positional ones at or near the start of my scripts.

As I previously explained, I like to make my scripts portable where possible, so by having incron decode $@ we are passing the source folder to the script and we're passing the destination folder too, so we could easily reuse the same script multiple times without having to change anything in it. For example:

Code:
/home/incron/source IN_DELETE,IN_CLOSE_WRITE /home/incron/action.sh $@ $# $% /home/incron/dest
/home/incron/anothersource IN_DELETE,IN_CLOSE_WRITE /home/incron/action.sh $@ $# $% /home/incron/somewheredest
And this will work with them using exactly the same action.sh because it was written to be reusable.

In reality, if it was my system, I'd consider adding another parameter to the incrontab entries that would be $5 and used for THELOG so that I could have separate log files for each of the entries. I'd also alter the log file format a bit to have the entries on a single line with a timestamp.
 
1 members found this post helpful.
Old 08-26-2017, 11:48 AM   #33
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
need to get my head round it sorry but it will click

i have seen this -

Just a few warnings of things that caught me out when I started using incron.

You Can’t Monitor a Directory More than Once
If you try and monitor a directory more than once you will get an error similar to

Sep 23 23:24:02 localhost incrond[9787]: cannot create watch for user root: (16) Device or resource busy

if you want to monitor a dir for different actions use commas to separate the events e.g. ‘IN_CLOSE_WRITE,IN_DELETE’. Then if needed use the $% wildcard to pass the type of event that triggered the command to your script.

so i was thinking this as rsync can delete aswell

/source IN_CREATE,IN_DELETE,IN_CLOSE_WRITE rsync --delete $@/$#/$% /dest/$#

as $% is the option for multiple events?

thanks,

rob
 
Old 08-28-2017, 04:48 AM   #34
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,483

Rep: Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556
Quote:
Originally Posted by robertkwild View Post
need to get my head round it sorry but it will click


so i was thinking this as rsync can delete aswell

Code:
/source IN_CREATE,IN_DELETE,IN_CLOSE_WRITE rsync --delete $@/$#/$% /dest/$#
as $% is the option for multiple events?
$% isn't "the option for multiple events" it's decoded in to the text version of the event type that caused the trigger

So if you have your incrontab entry like you've said above when you delete testfile a file incrond will attempt to run:

Code:
rsync --delete /source/testfile/IN_DELETE /dest/testfile
which doesn't make sense.

Similarly when you create a file you may fire multiple events for the IN_CREATE and IN_CLOSE_WRITE so it'll attempt:
Code:
rsync --delete /source/testfile/IN_CREATE /dest/testfile
rsync --delete /source/testfile/IN_CLOSE_WRITE /dest/testfile
which again, don't make sense.

If you're still determined to use rsync and process the whole folder then you could try:
Code:
/source IN_DELETE,IN_CLOSE_WRITE rsync -a --delete $@/ /dest/

Last edited by TenTenths; 08-28-2017 at 04:59 AM.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Transmission watch folder not working Vodkaholic1983 Linux - Software 1 05-25-2013 08:45 AM
How to set up public folder/ share folder for my network? Kiwi89 Linux - Server 6 10-31-2011 05:10 AM
rsync local web folder to remote web folder? Almost working... Ultrus Linux - General 2 03-25-2009 02:49 PM
rtorrent watch folder, NFS cormack Linux - Software 2 05-19-2007 01:47 PM
Rhythmbox and a folder to watch? fictos Linux - Software 5 11-17-2005 10:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS

All times are GMT -5. The time now is 06:23 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