Reputation: 152284
There is /var/log/httpd/error_log
file which contains errors from the whole httpd
.
I want to be able to send those new log via email message if any appears - with for example 15 minutes interval. I would like to use CRON
to call bash script that will send new content.
Is there any efficience way to obtain, which lines were appended since last check ?
Upvotes: 1
Views: 630
Reputation: 63972
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)" "[email protected]"
last_log_check_point_string
error_log
- the new marker_point
)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)" "[email protected]" < $TEMPFILE
rm -f "$TEMPFILE"
Upvotes: 2
Reputation: 3688
When sending a mail, you can store the line count of the file somewhere (e.g., a file), and then use tail +n
to print only from the n-th line on, like
last_linecount=`cat .last_linecount` # or check if file does not exist
wc -l /var/log/httpd/error_log >.last_linecount
tail -n +$((last_linecount + 1)) /var/log/httpd/error_log | whatever
You should also check if the current number of lines is lower than last_linecount
, as then the logfile might have been rotated (if applicable) and you have to combine both the tail on the old logfile and everything from the new logfile.
Upvotes: 2