LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-25-2010, 10:17 PM   #1
hussa1n
LQ Newbie
 
Registered: Jun 2010
Location: India
Distribution: RHEL5, Ubuntu
Posts: 13

Rep: Reputation: 0
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.
 
Old 06-25-2010, 11:08 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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
 
1 members found this post helpful.
Old 06-25-2010, 11:22 PM   #3
hussa1n
LQ Newbie
 
Registered: Jun 2010
Location: India
Distribution: RHEL5, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
Thanx for the reply @ kbp

Will this above script kill and start the process "myproc"
 
Old 06-25-2010, 11:39 PM   #4
hussa1n
LQ Newbie
 
Registered: Jun 2010
Location: India
Distribution: RHEL5, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
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.

Last edited by hussa1n; 06-25-2010 at 11:42 PM. Reason: mistake
 
Old 06-26-2010, 01:26 AM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

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

cheers
 
1 members found this post helpful.
Old 06-26-2010, 04:13 PM   #6
hussa1n
LQ Newbie
 
Registered: Jun 2010
Location: India
Distribution: RHEL5, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks kbp.
I really needed this one.
 
Old 06-29-2010, 04:20 PM   #7
hussa1n
LQ Newbie
 
Registered: Jun 2010
Location: India
Distribution: RHEL5, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
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.
 
Old 06-29-2010, 05:21 PM   #8
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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
 
Old 06-29-2010, 05:49 PM   #9
hussa1n
LQ Newbie
 
Registered: Jun 2010
Location: India
Distribution: RHEL5, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks kbp. That works fine.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to monitor files created on a folder cmadiam82 Programming 9 05-11-2013 12:59 AM
[SOLVED] bash script - check process is running, if not start zee# Linux - Newbie 8 02-07-2010 05:50 PM
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM
Bash script to check for dead process MaffooClock Other *NIX 12 05-18-2007 05:14 AM
How can I check wether a file is empty in Linux by using shell script programming ??? Bassam Linux - General 2 02-26-2004 01:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration