LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need to monitor a dir for files (https://www.linuxquestions.org/questions/linux-newbie-8/need-to-monitor-a-dir-for-files-4175636230/)

iqb.bada@gmail.com 08-12-2018 12:41 PM

Need to monitor a dir for files
 
Need to monitor a dir for files
if count > 0, sleep for 3min and check again for count and if count >0 then write to logfile.

if count = 0 then write to logfile(no files available)

scasey 08-12-2018 12:46 PM

Welcome to LQ

OK. Not sure what your question is...

What have you tried?
Please review the link in my signature, especially the link there about how to ask a question.

iqb.bada@gmail.com 08-12-2018 12:54 PM

Quote:

Originally Posted by scasey (Post 5890929)
Welcome to LQ

OK. Not sure what your question is...

What have you tried?
Please review the link in my signature, especially the link there about how to ask a question.

Hi
I tried the below

#!/bin/bash
dir="/tmp/test1/test"
LOG_FILE="/tmp/test1/files_mon.log"
>$LOG_FILE
count=`find $dir -type f -ls | wc -l`
if [ $count -ge 1 ]; then
sleep 10s
count=`find $dir -type f -ls | wc -l`
if [ $count -ge 1 ];
then
echo -e "Error : Files are not processed under dir $dir " >>$LOG_FILE
else
echo "INFO : All files are processed under dir $dir">>$LOG_FILE
fi
fi

smaclennan 08-12-2018 01:17 PM

That script looks fine... except you don't deal with the == 0 case. What is the problem?

scasey 08-12-2018 01:48 PM

Quote:

Originally Posted by iqb.bada@gmail.com (Post 5890932)
Hi
I tried the below

Code:

#!/bin/bash
dir="/tmp/test1/test"
LOG_FILE="/tmp/test1/files_mon.log"
>$LOG_FILE 
count=`find $dir -type f -ls | wc -l`
if [ $count -ge 1 ]; then
    sleep 10s
    count=`find $dir -type f -ls | wc -l`
    if [ $count -ge 1 ];
    then
        echo -e "Error : Files are not processed under dir $dir " >>$LOG_FILE
    else
        echo "INFO : All files are processed under dir $dir">>$LOG_FILE
  fi
fi


Please use [code] tags as above when posting code or output

Does that script not work? It looks like it should, except it doesn't match the specs in your OP. The sleep should be 180s or 3m (shorter for testing, perhaps?)

What is supposed to be happening in the directory that would change the count? If that's not happening, you'll never get the else. There are but two tests in 10 secs, then the script is done.

ondoho 08-13-2018 01:56 AM

wouldn't it be better to use inotifywait/inotifywatch for this?

smaclennan 08-13-2018 10:08 AM

Quote:

Originally Posted by ondoho (Post 5891100)
wouldn't it be better to use inotifywait/inotifywatch for this?

Quite possibly. But if it really does take minutes for the files to be processed, the script as shown is pretty simple with no real parsing necessary.


All times are GMT -5. The time now is 11:05 AM.