You can use the error_log
as a marker and don't need store line numbers in the external file.
the next script:
#!/bin/bash
STRING="last_log_check_point_string"
LOGFILE="/opt/local/apache2/logs/error_log"
URL="http://localhost/$STRING"
linenum=$(grep -n "$STRING" $LOGFILE | tail -1 | sed 's/:.*//')
curl "${URL}" >/dev/null 2>&1
[[ ! -z "$linenum" ]] && sed -n "$linenum,\$p" < $LOGFILE | mail -s "error log at $(date)" "your@email.com"
- will find the line number of the last occurence of
last_log_check_point_string
- will make an request to your web with nonexistent url what contain your string (so, you get an new notice into the
error_log
- the new marker_point
)
- every line after the above line number will mail to you.
The last line of the error_log will be the new marker point
, so you have a control how the script works. If you don't have curl
, use wget
- or is possible make a request with a pure bash too.
or the variant - you will get email only when some errors occured
#!/bin/bash
STRING="last_log_check_point_string"
LOGFILE="/opt/local/apache2/logs/error_log"
URL="http://localhost/$STRING"
TEMPFILE="/tmp/hterror.$$"
linenum=$(grep -n "$STRING" $LOGFILE | tail -1 | sed 's/:.*//')
curl "${URL}" >/dev/null 2>&1
[[ ! -z "$linenum" ]] && sed -n "$linenum,\$p" < $LOGFILE | grep -v "$STRING" >$TEMPFILE
[[ -s "$TEMPFILE" ]] && mail -s "error log at $(date)" "your@email.com" < $TEMPFILE
rm -f "$TEMPFILE"