Lists the last (most recent) 20 lines of the Apache2 web access log and error log, writing output to stdout, along with some general header and footer lines (including a line with date and time).
When run without any arguments, output is simple plain
text, suitable for viewing in a command prompt environment.
When run with -h argument, output is a complete HTML page
suitable for viewing with a web browser. The page includes a
meta tag to auto-refresh itself every 30 seconds.
Intended to be run (with the -h option) regularly (and fairly frequently) via cron, with the resulting web page going directly into a web site directory.
Click here to view the web page thus generated by the most recent run of this script (if it is being run on the machine you are viewing, that is).
1 #!/bin/bash 2 # run-weblog.sh 3 # 4 # L. J. Jaeckel 5 # CMPSC-210 -- Linux Administration 6 # April 22, 2011 7 # 8 # Lists the final (most recent) 20 lines of the apache2 web access 9 # and error logs. Output is written (along with a header line giving 10 # date and time) to stdout. 11 # 12 # If run with -h option, additional output is written to 13 # create a complete HTML page. 14 # 15 # Intended to be run regularly (and fairly frequently) via cron. 16 17 18 if [ "$1" = "-h" ] ; then 19 DO_HTML="yes" 20 HTDASH='—' 21 BEGINH1='<h1><center>' 22 ENDH1='</center></h1>' 23 BEGINH3='<h3>' 24 ENDH3='</h3>' 25 else 26 HTDASH='--' 27 fi 28 29 NLINES=20 30 REFRESH=30 31 WEB_ACC_LOG=/var/log/apache2/access.log 32 WEB_ERR_LOG=/var/log/apache2/error.log 33 MY_DATE=`date '+%T %A %B %-d, %Y'` 34 35 if [ $DO_HTML ] ; then 36 echo '<html>' 37 echo ' <head>' 38 echo " <title>Recent Web Access Log messages $HTDASH $MY_DATE</title>" 39 echo " <meta http-equiv=\"refresh\" content=\"$REFRESH\" />" 40 echo ' </head>' 41 echo '<body>' 42 fi 43 44 echo '' 45 echo "${BEGINH1}Recent Web Access Log messages $HTDASH (last $NLINES lines)$ENDH1" 46 47 if [ $DO_HTML ] ; then 48 echo "<center>Refreshing every $REFRESH seconds.</center>" 49 echo '<center>' 50 echo 'Click <a href="run-weblog-sh-list.html">here</a>' 51 echo 'to see the source listing of the shell script that' 52 echo 'creates this page, which should be run periodically' 53 echo 'as a cron task.<br />' 54 echo 'For a PHP version of this, which updates more' 55 echo 'regularly and is fancier too, take a look at' 56 echo '<a href="run-weblog.php">run-weblog.php</a>' 57 echo '</center>' 58 else 59 echo '' 60 fi 61 62 echo "${BEGINH3}${MY_DATE} $HTDASH Web Access messages:${ENDH3}" 63 echo '' 64 if [ $DO_HTML ] ; then 65 echo '<pre>' 66 fi 67 68 tail -$NLINES $WEB_ACC_LOG 69 70 if [ $DO_HTML ] ; then 71 echo '</pre>' 72 echo '<hr />' 73 echo '</body>' 74 echo '</html>' 75 else 76 echo '' 77 fi 78 79 echo "${BEGINH3}${MY_DATE} $HTDASH Web Error messages:${ENDH3}" 80 echo '' 81 if [ $DO_HTML ] ; then 82 echo '<pre>' 83 fi 84 85 tail -$NLINES $WEB_ERR_LOG 86 87 if [ $DO_HTML ] ; then 88 echo '</pre>' 89 echo '<hr />' 90 echo '</body>' 91 echo '</html>' 92 else 93 echo '' 94 echo ====================================================================== 95 fi 96 97 98 # End of this script -------------------------------------------------- 99