Script to monitor folder for new files?
Hi,
I got a problem regarding to making a script to monitor a folder for any new files. Im using Ubuntu 12.04 LTS My requirement is that: The script had to be constantly checking the folder for any new files. If detected i will then run a script to edit the new file. After editing the new file, the script had to go back to checking the folder again for any new file. I got a link that has what i wanted but when i tried it out, it cannot work. http://unix.stackexchange.com/questi...-for-new-files This is the code: Code:
inotifywait -m /path 2>&- | awk '$2 == "CREATE" { print $3; fflush() }' | Code:
inotifywait -m /home/user/output 2>&- | awk '$2 == "CREATE" { print $3; fflush() }' | Or is there any other solution? |
Check if folder have new files?
Hi,
I have a problem regarding checking if a folder has new files? My problem is that the script has to keep checking a folder for any new files and if detected new files, i will then run a script to edit the new file. I found a similar script that suit my needs but it does not seem to be working. The script that i found is: Code:
inotifywait -m /path 2>&- | awk '$2 == "CREATE" { print $3; fflush() }' | Code:
inotifywait -m /home/user/output 2>&- | awk '$2 == "CREATE" { print $3; fflush() }' | Or is there any other way? |
Do you get any errors when you run the script?
Have you verified the command, inotifywait, exists and is in your PATH? |
Quote:
I had already installed inotify and what do you mean by verifying the command, inotifywait, exists and is in your PATH? |
Welcome to LinuxQuestions.
What distribution/version are you running? As stated inotifywait may not be in your environment path. The environment path is a list of directories that is searched to locate an application for example when it is executed. I would assume it is /usr/bin. I would specify the event so all you need to do is have awk print out the filename. /usr/bin/inotifywait -e create -mrq /home/user/output | awk '{print $3; fflush() }' | while... Start the program in one terminal. Open up a second and run the command touch /home/user/output/test.me The file will be created and you should see echoed in the first terminal. |
Quote:
By using synaptic, i installed inotifywait. |
Type "which inotifywait" to see if it shows you the directory the command is in. If it does then it exists and is in your PATH. If it doesn't then it may exist but not be in your PATH.
The PATH variable determines where commands are located when you run them. You can see your PATH by typing "echo $PATH". The output will be a colon delimited list of directories such as: /bin:/usr/bin:/usr/local/bin:/opt/someapp When you type a command without putting in the full path then it searches through the PATH variable directories in order until it finds it. If your PATH had what I showed above but the intotifywait command was actually in something like /usr/share/bin it wouldn't find it when you typed it because that directory is not in your PATH. Every time you ran the command you'd have to type /usr/share/bin/inotifywait if it wasn't in your PATH. Since you say you installed it the command presumably exists - you just need to know where and be sure it is in PATH. |
Quote:
Code:
/usr/bin/inotifywait Code:
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games |
Well, i think i found another method to check if the folder have new files and do something about it.
First, i use watch to check a script every second. Next, the script will check for the folder if it has files in it. Because, at first the folder is empty. If, there files detected, the script will do something about it. Code:
#!/bin/sh Code:
if [ $(ls -1A output | wc -l) -gt 0 ] ;then "do_something"; fi |
Do you really need to fire off a script every second; that's a lot of overhead on your system.
Why not just create simple daemon Code:
while 1 Code:
nohup do_whatever & How soon/how often does it expect to find/need to process a file. If its going to loop quickly, use the nice cmd, so it doesn't dominate. |
Try adding the '-e close_write' option to inotifywait so that no action is taken until the file is actually written and closed.
I would also try the -q option to inotifywait rather than the error redirection. I think the awk stuff is redundant. Code:
inotifywait -mq -e close_write /home/user/output | \ |
All times are GMT -5. The time now is 08:45 AM. |