LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash shell script to check the if empty files are being created & start the process (https://www.linuxquestions.org/questions/linux-general-1/bash-shell-script-to-check-the-if-empty-files-are-being-created-and-start-the-process-816447/)

hussa1n 06-25-2010 10:17 PM

Bash shell script to check the if empty files are being created & start the process
 
I have an Ubuntu server in which a file is dumped every hour and a new file for the next hour and the process continues. If there is any problem due to which the creation of file stops then empty files are created every minute till the process is killed & started again. I need help to make a shell script to check if the empty files are being created and then kill the process and start it again.

It would be a great help if anyone can help me regarding this.

kbp 06-25-2010 11:08 PM

Give this a try:

Code:

#!/bin/bash

for file in /tmp/test/*
do
        if [[ ! -s $file ]]
        then
                echo "Found zero length file"
                pkill -9 myproc
        fi
        rm -f $file
done

cheers

hussa1n 06-25-2010 11:22 PM

Thanx for the reply @ kbp

Will this above script kill and start the process "myproc"

hussa1n 06-25-2010 11:39 PM

I'm kinda new to shell scripting and I don't have much idea about it. It would be a great help if you could help me.

In my scenario I need to run a script present in the home directory after th process is killed. Can I do it after changing your script as

Code:

#!/bin/bash

for file in /tmp/test/*
do
        if [[ ! -s $file ]]
        then
                echo "Found zero length file"
                pkill -9 myproc
                ./proc.sh
        fi
        rm -f $file
done

Thanks in advance.

kbp 06-26-2010 01:26 AM

try '~/.proc.sh' .. this is the home directory of the current user or you can use the full path .. '/home/<user>/proc.sh'

cheers

hussa1n 06-26-2010 04:13 PM

Thanks kbp.
I really needed this one.

hussa1n 06-29-2010 04:20 PM

When is use this script everything is fine. But the problem is that all the files including data files and empty files are being removed from /tmp/test/
Code:

#!/bin/bash

for file in /tmp/test/*
do
        if [[ ! -s $file ]]
        then
                echo "Found zero length file"
                pkill -9 myproc
                /home/user/proc.sh
        fi
        rm -f $file
done

How can this script be modified in order to delete only the empty files.

kbp 06-29-2010 05:21 PM

Sorry.. you didn't specify that you wanted to keep the non-empty ones :) .. try this:
Code:

#!/bin/bash

for file in /tmp/test/*
do
        if [[ ! -s $file ]]
        then
                echo "Found zero length file"
                pkill -9 myproc
                /home/user/proc.sh
                rm -f $file
        fi
done


hussa1n 06-29-2010 05:49 PM

Thanks kbp. That works fine.


All times are GMT -5. The time now is 08:28 AM.