CentOSThis forum is for the discussion of CentOS Linux. Note: This forum does not have any official participation.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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! )
#!/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 .
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.
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.
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
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.
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).
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
$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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.