[SOLVED] Bash script to check if file is present or not, check periodically every 30 mins
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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
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 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.
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
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
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.