Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuously monitor a directory in linux and notify when a new file is available

I'm a starter in Linux and scripting environment. My requirement is like this:

  1. From an asp.net application, a file will be generated and copied to a predefined folder on a Linux server machine. (I'm assuming this can be done by remote file sharing using samba server)

  2. A service or script or whatever should be there in Linux machine to track continuously whether the file is available.

  3. Once a new file is available, just parse the file, extract some input variables and execute a shell script based on these parameters.

My question lies in point no:2. --> How can I write a service or script which should execute continuously and monitor whether a file is available in a particular folder?

I've searched a lot, got into a lot of links and I'm confused what is the easiest method to do this. Because I don't want to spend a lot of coding here as the script to be executed further and the asp.net app is more important and this should be a connector in between.

like image 956
HRM Avatar asked Sep 09 '13 05:09

HRM


People also ask

How do I monitor a directory in Linux?

In Linux, we can use the inotify interface to monitor a directory or a file. We do this by adding a watch to the directory or file. When we add a watch to a file, we can monitor it. For example, we'll know when a process opens, modifies, reads closes, moves, or deletes the file.

How will you monitor file change in Linux?

In Linux, the default monitor is inotify. By default, fswatch will keep monitoring the file changes until you manually stop it by invoking CTRL+C keys. This command will exit just after the first set of events is received. fswatch will monitor changes in all files/folders in the specified path.

How do you loop a directory in Linux?

We use a standard wildcard glob pattern '*' which matches all files. By adding a '/' afterward, we'll match only directories. Then, we assign each directory to the value of a variable dir. In our simple example, we then execute the echo command between do and done to simply output the value of the variable dir.

How do you perform a task when a new file is added to a directory in Linux?

Watching a Directory and Executing a TaskThe script executes the inotifywait command with the -m option. This makes the command monitor changes indefinitely. Each time a new event is detected, the filename is passed to the read command and injected into the “FILENAME” variable.


2 Answers

You are looking for something like inotify.

[cnicutar@ariel ~]$ inotifywait -m -e create ~/somedir/
Setting up watches.
Watches established.
/home/cnicutar/somedir/ CREATE somefile

For example, you could do it in a loop:

inotifywait -m -e create ~/somedir/ | while read line
do
    echo $line
done
like image 77
cnicutar Avatar answered Sep 28 '22 09:09

cnicutar


inotify is the perfect solution to your problem. It is available as a system command that can be used in a shell, or as a system call that can be used in a C/C++ program. For details see the accepted answer to this question: Inotify - how to use it? - linux

Update: you need inotify-tools for use on command line. The answer to the question above only describes C/C++ system calls. Here is the link to inotify-tools. It is also available as a packaged distribution so search your favorite install repository (yum/apt-get/rpm etc.): https://github.com/rvoicilas/inotify-tools/wiki

like image 45
necromancer Avatar answered Sep 28 '22 09:09

necromancer