1

Ultimately I want to detect a new backup file being dropped into a directory and then move that new file to another location for other operations.

This needs to work when there is nobody logged into the server and the script I use to start the operation will be triggered by a crontab entry.

I tried using 'inotifywait' like this:

dir=/home/userid/drop/
target=/home/userid/current/

inotifywait -m "$dir" --format '%w%f' -e create |
    while read file; do
        mv "$file" "$target"
    done

This only works in you have a terminal window session open. If you try to start this scipt unattended using a crontab entry, the inotifywait command is ignored.

So then I tried using 'entr' and found the same problem. It only works if you have a terminal window open. When I created a script using entr and triggered it unattended with a crontab entry, it was ignored just like the inotifywait example.

So I know that this can be done using 'stat' and I have looked at many examples and tried to figure them out for my purpose, but I am just not understanding the syntax for it.

I know stat can detect the existence of a new file in a directory, but I do not know how to process that result in order to trigger the execution of the mv (move) command to move the new file to a different location.

And once I have a stat syntax that can do this, I will need it to run perpetually. Maybe it only checks every 15 seconds or something, but I will need it to always be prepared to move the file.

If anyone has experience doing this and can kindly help me with the syntax to link stat to executing another command, I would be greatly appreciative. I really believe that others would like to know how to do this as well, because I cannot imagine that everyone is ok with keeping a ssh putty window open 24/7 for the other 2 solutions.

Thanks in advance.

BKM

2

All you need is incron. Install incron package first if you have Ubuntu/Debian:

sudo apt install incron

or use the command for Red Hat/Fedora:

sudo yum install incron

Open file /etc/incron.allow in your favorite text editor - let it be vim:

vim /etc/incron.allow

and add new line with your user name (assume it's bob) to allow him to use incron:

bob

Afterwards open incron rules editor:

incrontab -e

and add the following line:

/home/userid/drop/ IN_CREATE mv /home/userid/drop/$# /home/userid/current/

where $# is incron built-in wildcard which means name of newly dropped backup file detected by incron.

To test the created rule add a file to the /home/userid/drop/ directory and check if the dropped file has been moved to /home/userid/current/ directory. Additionally check syslog:

tail /var/log/syslog
  • This worked. It is not exactly what I was looking for, but it worked very well, so I have used this to solve my problem for now. I was hoping for an example of something using 'stat' as the file detector so I could use it on multiple systems without having to install another package. However, this worked better than I expected, so... Thank you! BKM – BKM Aug 18 '18 at 21:39
0

A periodic cron job is unsuitable as those run only occasionally while a process that reacts to inode changes must run all the time. An @reboot job that starts a process that (hopefully) runs forever should suffice.

$ crontab -l
@reboot /home/jhqdoe/job
$ cat job
#!/bin/sh
dir=/home/jhqdoe/drop/
target=/home/jhqdoe/current/
inotifywait -m "$dir" --format '%w%f' -e create \
| while read file; do mv -- "$file" "$target"; done
$ sudo reboot

A startup daemon managed by the init system would likely be a better option, if possible, as those are more easily restarted should the job fail for any number of reasons.

  • I tried this, in several permutations, and I could not get it to work. It still seems to have the same problem I was having before. The inotifywait command is ignored if it is started as unattended from a startup or cron triggered script. But I did give it an hour of my time again to try, just in case I missed something before. BKM – BKM Aug 18 '18 at 21:36
  • is your home directory encrypted or something otherwise not available by default? – thrig Aug 18 '18 at 22:50
  • No encrypted directories. Just cannot get inotifywait or entr to run unattended without a ssh window open. No matter how is start it with a startup boot script or one triggerd by a crontab entry. They just do not work. However the incron command did work so that is what I installed and am using now. – BKM Aug 18 '18 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.