Reputation: 81
I have configured a cron job where at one step it need's to execute a command where it will take 10 - 14 hrs of time. If it's completed successfully then we will get this "Build of target : PASS" in main.txt file. How do I need to check "Build of target : PASS" in main.txt file for every one hour ?
if grep -q "Build of target : PASS" main.txt; then
echo "Passed"
else
echo "failed"
fi
I got stuck at how check/grep the word to for every one hour.
Upvotes: 0
Views: 1390
Reputation: 15438
Maybe something like
res=failes
for h in 1 2 3 4 5 6 7 8 9 10 11 12 13 14
do if grep -q "Build of target : PASS" main.txt
then res="Passed"
break
else sleep 3600
fi
do
if [[ Passed != "$res" ]] && grep -q "Build of target : PASS" main.txt
then res="Passed"
fi
echo "$res"
If the process runs for the full 14 hours and only finished successfully near the end, then at the beginning the 14th hour the grep
will not find the PASS and will sleep, but at the end of the hour the for loop will not enter a 15th iteration to test it again. This checks for last-minute successes, but checks the string first so that it won't need to grep
again if already found.
A better way would probably be to flip that logic, since it's unlikely to have succeeded already at the beginning of the first hour -
res=failed
for h in 1 2 3 4 5 6 7 8 9 10 11 12 13 14
do sleep 3600
if grep -q "Build of target : PASS" main.txt
then res="Passed"
break
fi
done
echo "$res"
But be aware this guarantees a wait the first hour before it even checks.
Upvotes: 2
Reputation: 189937
Run an hourly cron job with the code to perform the checking.
17 * * * * grep -q "Build of target : PASS" main.txt && fuser -k main.txt
fuser -k
is just a simple way to kill whoever is writing to main.txt
on Linux; if you know the PID or are on another platform, there will be other ways to figure out which process exactly to kill.
Upvotes: 2