LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-26-2013, 08:02 PM   #1
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Rep: Reputation: 4
Bash script to check if file is present or not, check periodically every 30 mins


We have a scenario, like we need to check a particular file if its present or not. This file will be uploaded by an application any time. If its present fine or else it should continue to check every 30 mins, whether the file is present or not...

time period to be check between.
Start time 8 AM
End Time 12.30 PM.

#!/bin/bash
DATE=$(date +%Y%m%d)
file=`find /home/Test/resv_$DATE.dat.zip.pgp -mmin +30`
cd /home/Test/
if [ -e "resv_$DATE.dat.zip.pgp" ]; then
echo "Yes, file exists"
echo "resv_$DATE.dat.zip.pgp" > /usr/local/sysadm/FilePresent.txt
else
sleep 1800
if test $file
then
echo "Yes, file exists"
echo "resv_$DATE.dat.zip.pgp" > /usr/local/sysadm/FilePresent.txt
else
echo "No, file does not exist"
fi
fi



My Condition is like this
1. Check if file is present or not in the given location
2. If present it should update the file name in a text file in another location
3. If file not present in the given location, then it should check every 30 mins, whether the file is there or not.
4. While checking, if the file is present, then it must update the file name in the text file.



This is not working as per the requirement. Request anyone to correct me

Last edited by Iyyappan; 06-26-2013 at 08:04 PM.
 
Old 06-26-2013, 08:15 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For the timing ctrl, just put the script into cron.
Code:
*/30 * * * * /path/to/script.sh >/path/to/script.log 2>&1
Log everything in case of errors etc.

It seems to me you just want to check if a file exists and (if it does) tell another file about it
Code:
#!/bin/bash
DATE=$(date +%Y%m%d)
cd /home/Test/
if [[ -e "resv_${DATE}.dat.zip.pgp" ]]
then
    echo "resv_${DATE}.dat.zip.pgp" > /usr/local/sysadm/FilePresent.txt
fi
http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

See
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 06-27-2013, 09:45 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
May I suggest using inotifywait. This eliminates the need for a cron job and will immediately update the file name when it is created.

http://linux.die.net/man/1/inotifywait
http://linuxaria.com/article/introdu...notify?lang=en
 
2 members found this post helpful.
Old 06-30-2013, 07:24 AM   #4
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Original Poster
Rep: Reputation: 4
#!/bin/bash

DATE=$(date +%Y%m%d)
FILE=/home/tester
DSTFILE=/home/tester/resv_$DATE.dat.zip.pgp
cd $FILE
echo "resv_$DATE.dat.zip.pgp" > /usr/local/sysadm/FilePresent.txt
while true; do

inotifywait -e create $DSTFILE &&
echo $DSTFILE
done


But here its checking every sec, I would like to check every 30 mins between 8 - 12.30 PM
 
Old 06-30-2013, 11:51 AM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by Iyyappan View Post
#!/bin/bash

DATE=$(date +%Y%m%d)
FILE=/home/tester
DSTFILE=/home/tester/resv_$DATE.dat.zip.pgp
cd $FILE
echo "resv_$DATE.dat.zip.pgp" > /usr/local/sysadm/FilePresent.txt
while true; do

inotifywait -e create $DSTFILE &&
echo $DSTFILE
done


But here its checking every sec, I would like to check every 30 mins between 8 - 12.30 PM
what did you use to start the program? if you put it in cron then you can tell it something like the following:
Code:
*/30 8-12 * * * /path/to/script
ps, please use "code" "/code" flags (replacing " " with [ ] ) thank you.
 
Old 06-30-2013, 05:34 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
inotifywait constantly monitors the specified directory for new files so a cron job is not required. You can start / stop the the bash script via a cron job at the desired times otherwise of constantly monitoring is not desired then lleb post will work.
 
1 members found this post helpful.
Old 07-01-2013, 12:40 AM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
good to know. thanks.
 
Old 07-02-2013, 08:46 AM   #8
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Original Poster
Rep: Reputation: 4
File will dropped in /home/tester at any time between 8.00 AM - 8.00 PM.
DATE=$(date +%Y%m%d)
if time is between 8.00 AM and 8.00 PM then go to below commands.

then

cd /home/Test/
if [[ -e "resv_${DATE}.dat.zip.pgp" ]]
then
echo "resv_${DATE}.dat.zip.pgp" > /usr/local/sysadm/FilePresent.txt
fi

else
No need to do anything.

*** I need to know, how to check if time is between 8.00 AM and 8.00 PM and get to the other commands. I want the script to get in the second loop only if time is between 8.00 AM and 8.00 PM, no need to check in time frame apart

Last edited by Iyyappan; 07-02-2013 at 09:02 AM.
 
Old 07-03-2013, 01:51 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
See the date man page; you can specify all sorts of time formats to be output.
 
Old 07-03-2013, 03:25 AM   #10
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Original Poster
Rep: Reputation: 4
This is working fine for me


#!/bin/bash
DATE=`date +%k%M`
date=$(date +%Y%m%d)
echo $date

check_time_to_run()
{
tempTime=$1
if [ $tempTime -gt 800 -a $tempTime -lt 2000 ]; then
echo "Time is between 8 AM and 8PM."
cd /home/tester
if [[ -e "resv_${date}.dat.zip.pgp" ]];then
echo "resv_${date}.dat.zip.pgp" >> /usr/local/sysadm/FilePresent.txt
echo "resv_${date}.dat.zip.pgp"
echo "1"
else
echo "0"
fi
else
echo "No need to check"
fi
}

check_time_to_run $DATE
 
Old 07-03-2013, 05:19 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Glad to hear it
If this is complete, please mark it as solved.
Also, please use code tags https://www.linuxquestions.org/quest...do=bbcode#code in future; its much easier to read.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
writing bash script to check if file present on that partition sumeet inani Linux - Newbie 10 06-14-2013 02:42 PM
bash: check periodically until 'virsh blockjob dom vda' shows 100% completion himpierre Programming 2 06-03-2013 06:14 AM
Script to check PID from file and check whether process is running or not rajkiran183 Linux - Newbie 5 10-19-2012 11:28 AM
Bash Shell Script - Check SQL file for corruption RML1992 Linux - General 3 09-14-2012 12:47 AM
bash script file access check? viperuk Linux - Newbie 1 12-10-2010 08:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:24 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