Home > front end >  Script to monitor a certain amount of files in a directory in bash
Script to monitor a certain amount of files in a directory in bash

Time:01-02

I'm breaking my head with a simple script and I need help.

I have the following scenario:

I have a directory to monitor where the script will run until the number of files reaches the expected number (exit 0) or the expiration time arrives (exit 1)


Rules:

  1. Number of files hit, exit 0
  2. Time limit hit even without the amount of files reached, exit 0

Usage: ./filemonitor.sh <file_mask> <file_amount> <expire_time>

Eg : ./filemonitor.sh /home/abc/ dl*.txt 5 14

I don't know if I made myself clear but what am I doing wrong?

Script


#!/bin/bash

# Variables
now=$(date  "%H")

# User input
files_path=$1
files_msk=$2
files_qtde=$3
files_lmttime=$4

check() {

    while :; do

        file_count=$(find $files_path -mindepth 1 -maxdepth 1 -type f -iname "$files_msk" | wc -l)

        if [ $file_count -lt $files_qtde ]; then

            if [ $now -ge $files_lmttime ]; then

                echo 'Time is over!'
                exit 1

            elif [ $now -lt $files_lmttime ]; then

                echo 'On time, process running'
                echo 'Wait 5 secs'
                sleep 5

            fi

        elif [ $file_count -eq $files_qtde ]; then

            echo "file count="$file_count "file expected="$files_qtde
            echo 'Files OK'
            exit 0

        fi

    done
}

check

CodePudding user response:

Problem solved. Thanks for all.

# Check user to run
if [[ $USER != "abc" ]]; then
    echo "Abc user needed to run!"
    exit 1
fi

# Variables
now=$(date  "%H")
pause_time=2
log_path='/home/abc/logs/'
log_time=$(date  "%Y-%m-%d %H:%M:%S")
log_file=$log_path$(date  "%Y%m%d")"_filemonitor.log"

# User input
files_path=$1
files_msk=$2
files_qtde=$3
files_lmttime=$4

# Check if log path exist
if [ ! -d $log_path ]; then
    echo 'Log directory does not exist. Creating...'
    mkdir -p $log_path
fi

# Check if log file exist
if [ ! -f $log_file ]; then
    echo 'Log file does not exist. Creating...'
    touch $log_file
fi

check() {

    while :; do
        # Count how many files are in the directory
        file_count=$(ls -1 $files_path | wc -l)

        # Checks if the number of files in the directory is less than the expected amount.
        if [ $file_count -lt $files_qtde ]; then

            # Checks if the current time is less than the expiration time passed by the user
            if [ $now -gt $files_lmttime ]; then
                echo -e $log_time "Waiting for files. Wait $pause_time secs" >>$log_file
                sleep $pause_time
            else
                # Cancels the process due to time expired
                echo -e $log_time 'Time is over!' >>$log_file
                echo -e $log_time "Error" >>$log_file
                exit 1
            fi
        # Checks if the number of files equals the expected number
        elif [ $file_count -eq $files_qtde ]; then
            echo -e $log_time "Files OK" >>$log_file
            echo -e $log_time "Process OK" >>$log_file
            exit 0
        # Checks if the number of files is greater than the expected number
        elif [ $file_count -gt $files_qtde ]; then

            # Checks if the current time is less than the expiration time passed by the user
            if [ $now -gt $files_lmttime ]; then
                echo -e $log_time "Number of files larger than expected." >>$log_file
                echo -e $log_time "[$files_path] contains = "$file_count "files" >>$log_file
                echo -e $log_time "Error" >>$log_file
                exit 1
            else
                # Cancels the process due to time expired
                echo -e $log_time 'Time is over!' >>$log_file
                echo -e $log_time "Error" >>$log_file
                exit 1
            fi
        fi
    done
}

check
  • Related