Page 1 of 1

Performing an action after a file is written to a directory on RPi4?

Posted: Wed Jul 15, 2020 8:44 am
by Bosse_B
I would like to know if it is possible to run a script whenever a new file gets written to a directory?
Ideally the script should run say a couple of minutes following the writing of the file and if multiple files have been written during that time the script should only run once.

Is that possible on the RPi4?

The directories I want to monitor are located on a USB connected large NTFS formatted drive (1 TB).
If possible the monitoring should only watch specific file types being written (*.cpp, *.pas, *.mp4 etc)

And the monitoring should run even when noone is logged on to the system.
(Because file writes may be done via the samba share)

Re: Performing an action after a file is written to a directory on RPi4?

Posted: Wed Jul 15, 2020 8:59 am
by jahboater
Take a look at:

man inotify

Re: Performing an action after a file is written to a directory on RPi4?

Posted: Wed Jul 15, 2020 9:21 am
by Bosse_B
jahboater wrote:
Wed Jul 15, 2020 8:59 am
Take a look at:
man inotify
So I checked if inotify was available:

Code: Select all

pi@rpi4-gui:~ $ which inotify
pi@rpi4-gui:~ $
But still this worked to bring up a description:

Code: Select all

pi@rpi4-gui:~ $ man inotify
But it is too dense for me. I cannot see how it can be run from a script...

It seems from the example like you have to create a C program which then needs to be executed and output is written to the terminal.
But I need this to be a function that is running in the background even if there is no-one logged on to the RPi4.

A little like cron, in fact a cron execution of a checking script could be a solution provided it can determine if there have been changes to the monitored directories between cron invocations.

Re: Performing an action after a file is written to a directory on RPi4?

Posted: Wed Jul 15, 2020 9:29 am
by dickon
So write a small program which uses inotify to watch the directory and fork a script when it changes. Run that at startup as a service, or an @reboot cron job.

It isn't rocket science.

Or write a script which runs from cron once a minute that checks the mtime of the directory, which may (or may not) be good enough for your needs. A fire-on-close inotify triviality is probably better, though. Note that most[0] scripting languages have bindings.

[0] Perl does, anyway. That's good enough for 'most', IMHO.

Re: Performing an action after a file is written to a directory on RPi4?

Posted: Wed Jul 15, 2020 10:12 am
by rpdom
Bosse_B wrote:
Wed Jul 15, 2020 9:21 am
So I checked if inotify was available:
If you are wanting to use inotify from a script, rather than the default C program, you will probably need to install inotify-tools, which will give you a scriptable command line interface.

Re: Performing an action after a file is written to a directory on RPi4?

Posted: Wed Jul 15, 2020 10:53 am
by philrandal
I've had problems with inotify in the past (not on Pis) and ended up using systemd functionality:

https://www.linux.com/topic/desktop/sys ... rectories/

https://zerokspot.com/weblog/2018/09/15 ... h-systemd/

Cheers,

Phil

Re: Performing an action after a file is written to a directory on RPi4?

Posted: Wed Jul 15, 2020 12:23 pm
by tpylkko
You can do this directly with systemd. You create a .path file

Just Google "monitor directory systemd path unit"