LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 04:01 AM   #16
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,476

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553

Right, I'm back in the office Grab yourself a coffee, I did, and let's get started!

For this example I'm using /home/incron/source as the source (clue is in the name! ) and /home/incron/dest as the destination (clue is also in the name! )

Code:
# incrontab -l
/home/incron/source IN_DELETE,IN_CLOSE_WRITE /home/incron/action.sh $@ $# $% /home/incron/dest
action.sh:
Code:
#!/bin/bash

# Author : Grant MacDonald
# Purpose: Action handler for files in folders
#        : https://www.linuxquestions.org/questions/centos-111/set-up-a-watch-folder-or-rsync-4175612499/
# Version: 1.0
# Date   : 25/08/2017

# Version History
# 1.0 Initial version

# Needs the following incrontab -e entry
# /home/incron/source IN_CLOSE_WRITE,IN_DELETE /home/incron/action.sh $@ $# $% /home/incron/dest

# incrontab uses the following:

# $$   dollar sign
# $@   watched filesystem path (see above)
# $#   event-related file name
# $%   event flags (textually)
# $&   event flags (numerically)

# Define some variables based on the command line
THEFOLDER=$1
THEFILE=$2
THEACTION=$3
THEDEST=$4
THELOG=/home/incron/action.log

echo "File:   ${THEFILE}" >> ${THELOG}
echo "Source: ${THEFOLDER}" >> ${THELOG}
echo "Action: ${THEACTION}" >> ${THELOG}
echo "Dest:   ${THEDEST}" >> ${THELOG}

case ${THEACTION} in

  IN_CLOSE_WRITE) # A file has been closed after change
                  echo "cp -f ${THEFOLDER}/${THEFILE} ${THEDEST}/${THEFILE}" >> ${THELOG}
                  cp -f ${THEFOLDER}/${THEFILE} ${THEDEST}/${THEFILE}
  ;;

  IN_DELETE) # A file has been deleted
             echo "rm -f ${THEDEST}/${THEFILE}" >> ${THELOG}
             rm -f ${THEDEST}/${THEFILE}
  ;;

esac

# Elvis Has Left The Server
Now we try it:
Our folders are pretty much empty:
Code:
[someuser@somehost incron]# ls -lath
total 4.0K
drwxr-xr-x  4 someuser someuser   46 Aug 25 09:41 .
drwxr-xr-x  2 someuser someuser    6 Aug 25 09:40 dest
drwxr-xr-x  2 someuser someuser    6 Aug 25 09:40 source
-rwxr-xr-x  1 someuser someuser 1.1K Aug 25 09:40 action.sh
drwxr-xr-x. 5 someuser someuser   61 Aug 25 09:32 ..

[someuser@somehost incron]# ls -lath source
total 0
drwxr-xr-x 4 someuser someuser 46 Aug 25 09:41 ..
drwxr-xr-x 2 someuser someuser  6 Aug 25 09:40 .

[someuser@somehost incron]# ls -lath dest
total 0
drwxr-xr-x 4 someuser someuser 46 Aug 25 09:41 ..
drwxr-xr-x 2 someuser someuser  6 Aug 25 09:40 .
Now we create a file in the source folder
Code:
[someuser@somehost incron]# echo "Test File 1" > source/testfile1
Now we see the file created in source, in dest and we have a log file!
Code:
[someuser@somehost incron]# ls -lath source
total 4.0K
drwxr-xr-x 4 someuser someuser 63 Aug 25 09:41 ..
drwxr-xr-x 2 someuser someuser 22 Aug 25 09:41 .
-rw-r--r-- 1 someuser someuser 12 Aug 25 09:41 testfile1

[someuser@somehost incron]# ls -lath dest
total 4.0K
drwxr-xr-x 2 someuser someuser 22 Aug 25 09:41 .
-rw-r--r-- 1 someuser someuser 12 Aug 25 09:41 testfile1
drwxr-xr-x 4 someuser someuser 63 Aug 25 09:41 ..

[someuser@somehost incron]# ls -lath
total 8.0K
drwxr-xr-x  2 someuser someuser   22 Aug 25 09:41 dest
-rw-r--r--  1 someuser someuser  159 Aug 25 09:41 action.log
drwxr-xr-x  4 someuser someuser   63 Aug 25 09:41 .
drwxr-xr-x  2 someuser someuser   22 Aug 25 09:41 source
-rwxr-xr-x  1 someuser someuser 1.1K Aug 25 09:40 action.sh
drwxr-xr-x. 5 someuser someuser   61 Aug 25 09:32 ..

[someuser@somehost incron]# cat action.log
File:   testfile1
Source: /home/incron/source
Action: IN_CLOSE_WRITE
Dest:   /home/incron/dest
cp -f /home/incron/source/testfile1 /home/incron/dest/testfile1
But we also want to delete files:
Code:
[someuser@somehost incron]# rm source/testfile1
rm: remove regular file ‘source/testfile1’? y
And now we look in the folders again and they are empty and we have more in the log
Code:
[someuser@somehost incron]# ls -lath source
total 0
drwxr-xr-x 2 someuser someuser  6 Aug 25 09:41 .
drwxr-xr-x 4 someuser someuser 63 Aug 25 09:41 ..

[someuser@somehost incron]# ls -lath dest
total 0
drwxr-xr-x 2 someuser someuser  6 Aug 25 09:41 .
drwxr-xr-x 4 someuser someuser 63 Aug 25 09:41 ..

[someuser@somehost incron]# cat action.log
File:   testfile1
Source: /home/incron/source
Action: IN_CLOSE_WRITE
Dest:   /home/incron/dest
cp -f /home/incron/source/testfile1 /home/incron/dest/testfile1
File:   testfile1
Source: /home/incron/source
Action: IN_DELETE
Dest:   /home/incron/dest
rm -f /home/incron/dest/testfile1
[someuser@somehost incron]#
Tested on CentOS Linux release 7.3.1611 (Core) and CentOS release 6.8 (Final)
 
1 members found this post helpful.
Old 08-25-2017, 04:48 AM   #17
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
wow thanks tentenths

but why shouldnt my 2 commands works just by the commands in the incrontab, why do i need to write a script for the two commands but if i write one command in the incrontab it works?

is there another program i could use that does this

is there anything that can do folders aswell as files

Last edited by robertkwild; 08-25-2017 at 05:00 AM.
 
Old 08-25-2017, 05:10 AM   #18
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,476

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by robertkwild View Post
wow thanks tentenths

but why shouldnt my 2 commands works just by the commands in the incrontab, why do i need to write a script for the two commands but if i write one command in the incrontab it works?

is there another program i could use that does this

is there anything that can do folders aswell as files
Not really looked in to why the two entry version didnt work, may have that as a coffee break problem.

As for sub-folders, I've never been able to easily make that work.
 
Old 08-25-2017, 05:22 AM   #19
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
i know inotifywait, only does files aswell, but i may just do a rsync command or copy command to do folders files aswell using the * to transfer all files/folders in the source, what do you think
 
Old 08-25-2017, 05:46 AM   #20
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,476

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
You could then set up your incrontab to look for a few different events based on your environment and rsync sure, it's your system, you'd have to see if rsync caused excessive overhead, I think you've probably learned enough to have a few ideas on things to try.
 
Old 08-25-2017, 06:35 AM   #21
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,740

Rep: Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921
inotifywait can be recursive i.e. watch subdirectories but as far as I know incron can not.

Not sure what you mean by only does files.

Don't know exactly how incron works but there might be some recursion happening because you have created two watches for the same directory.
 
Old 08-25-2017, 08:17 AM   #22
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
oh, i see micheal where your getting at, as i have created two watches for the same folder so it doesnt know what one to do first

i have read the man page and theres this -

Additionaly, there is a symbol which doesn't appear in the inotify symbol set. It it IN_NO_LOOP. This symbol disables monitoring events until the current one is completely handled (until its child process exits).

is this what i need?
 
Old 08-25-2017, 08:40 AM   #23
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,740

Rep: Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921
I don't know and never have tried it. I have always used scripts similar to what TenTenths posted to process events.
 
1 members found this post helpful.
Old 08-25-2017, 09:04 AM   #24
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,476

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Been trying this just for a laugh and it does seem that the first action defined in incrontab for a folder is the only one that will "fire"
 
Old 08-25-2017, 09:40 AM   #25
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
thanks tentenths for trying mate, appreciate it big time

as michael said probably its because there watching the same folder, i bet if they were different watch folders ie line one was source a and dest b and line 2 was source c dest d it would work

wouldnt the "IN_NO_LOOP" work?
 
Old 08-25-2017, 10:02 AM   #26
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,476

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
IN_NO_LOOP seems to break my Centos 6
 
Old 08-25-2017, 10:08 AM   #27
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
i dont know where to put that command, i will test tonight on my test rig at home

/mnt/invoicetest IN_CLOSE_WRITE cp -r $@/$# /mnt/invoicetest_1/$#
IN_NO_LOOP
/mnt/invoicetest IN_DELETE rm -f $@/$# /mnt/invoicetest_1/$#

or

/mnt/invoicetest IN_CLOSE_WRITE,IN_NO_LOOP cp -r $@/$# /mnt/invoicetest_1/$#
/mnt/invoicetest IN_DELETE,IN_NO_LOOP rm -f $@/$# /mnt/invoicetest_1/$#
 
Old 08-25-2017, 10:13 AM   #28
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,476

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by robertkwild View Post
/mnt/invoicetest IN_CLOSE_WRITE,IN_NO_LOOP cp -r $@/$# /mnt/invoicetest_1/$#
/mnt/invoicetest IN_DELETE,IN_NO_LOOP rm -f $@/$# /mnt/invoicetest_1/$#
Tried this on Centos 6 and Centos 7 and it doesn't have the desired result. To be honest you're better off with the scripted version.
 
Old 08-25-2017, 10:47 AM   #29
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
THEFOLDER=$1

cp -f ${THEFOLDER}/${THEFILE} ${THEDEST}/${THEFILE}

i just want to understand your script abit more -

cp -f ${THEFOLDER}/${THEFILE} ${THEDEST}/${THEFILE}

but the variable THEFOLDER=$1, what does $1 do?

as normal scripts i have done in the past

source=/mnt/invoicetest

so if you put in your script $source its really a shortcut to /mnt/invoicetest

thanks,

rob

Last edited by robertkwild; 08-25-2017 at 10:49 AM.
 
Old 08-25-2017, 12:13 PM   #30
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,476

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
$1 is the first variable passed on the command line. In this case the incrontab variable for the source folder is passed.
$2 is the filename again from the incrontab variable
$3 is the action in word form
$4 I pass the destination folder

Why don't I code source and dest in the script? Because I write portable scripts. You could have another line in incrontab for _source2 and a different destination folder as the 4th parameter and you dont have to edit another copy of the script.
 
  


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