I have this bash script whose job is to monitor a log file for the occurrence of a certain line. When located, the script will send out an email warning and then terminate itself. For some reason, it keeps on running. How can I be sure to terminate bash script below:
#!/bin/sh
tail -n 0 -f output.err | grep --line-buffered "Exception" | while read line
do
echo "An exception has been detected!" | mail -s "ALERT" monitor@company.com
exit 0
done
You are opening a subshell in the
while readand that subshell is who is exiting, not the proper one.Try before entering the while loop:
And then in the loop:
Or change your loop to not use a subshell.
Since the parent script is always going to be in the
tail -fwhich never ends I think you have no other choice than killing it from the inner subshell.