We have a file called status.txt which will have only 1 line. The content will be the string "Processing" for most of the day.
# cat status.txt
Processing
#
I want to write a shell script (notify.sh) which will be executing a grep every 10 minutes .
Once the script is executed , it should do the following
run a grep command every 10 minutes and check if the content of status.txt is anything other than "Processing" .
If the status.txt file has anything other than the string " Processing " (ie. grep -v "Processing" ) then it should echo "Completed". Actually in my real business scenario If the status.txt has anything other than "Processing" then a mail needs to be send out using Linux's mail command. But, for our testing purpose an echo "completed" should do .
Stop the execution of the notify.sh script (exit) after grep command managed to find anything other than the string " Processing " and printed " Complete " .
Note: I don't this to be invoked via a cronjob because in my real world scenario this logic has to be incorported as a subsection of a much bigger script.
Would you be better to schedule this with cron to run every 10 minutes, so that if your session crashes, the process will still fire up? You could easily code it something like:-
myvar="$(cat status.txt)"
if [ "$myvar" = "Processing"
then
# Take normal action here
else
# Take exception action here
fi
If the normal action is just to go back to sleep, then this simplifies to:-
myvar="$(cat status.txt)"
if [ "$myvar" = "Processing"
then
print "Still working" >> trace_file
exit 0
fi
# Take exception action here
Is this what you are after? If not, could you explain a little more?
I would worry that your script will not start again after a server boot, network hiccup or other trauma that stops it, hence why I suggest scheduling it as discreet test rather than the infinite loop.
The redirect will stop you getting messages to standard output (the screen for an on-line session) that you may not want. You could also dispose of it with:-
while true
do
if grep -qv Processing status.txt
then
echo Complete
exit
fi
sleep 600
done
It is the return code (i.e. did I match the expression/string or not) that is best here, rather than my ill-considered attempt to read the file into a variable each time.
It's simply a matter of taste. I used it to keep the terminal clean(er)
Sometimes one just doesn't care about the output because we only rely on the exit status, like in this case.
Anyways, &>/dev/null is not mandatory here.
---------- Post updated at 07:19 PM ---------- Previous update was at 07:14 PM ----------