L. J. Jaeckel
CMPSC 210 — Linux System Administration
April 22, 2011

Listing of shell script: run-dmesg.sh

Runs dmesg, writing the last (most recent) 20 lines 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 60 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-dmesg.sh
     3	#
     4	# L. J. Jaeckel
     5	# CMPSC-210 -- Linux Administration
     6	# April 22, 2011
     7	#
     8	# Runs dmesg (list of system messages), capturing the last
     9	# (most recent) 20 lines.  Output is written (along with
    10	# a header line giving 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=60
    31	MY_DATE=`date '+%T %A %B %-d, %Y'`
    32	
    33	if [ $DO_HTML ] ; then
    34	    echo '<html>'
    35	    echo '  <head>'
    36	    echo "    <title>Recent System Log messages $HTDASH $MY_DATE</title>"
    37	    echo "    <meta http-equiv=\"refresh\" content=\"$REFRESH\" />"
    38	    echo '  </head>'
    39	    echo '<body>'
    40	fi
    41	
    42	echo ''
    43	echo "${BEGINH1}Recent System Log (dmesg) messages $HTDASH (last $NLINES lines)$ENDH1"
    44	
    45	if [ $DO_HTML ] ; then
    46	    echo "<center>Refreshing every $REFRESH seconds &mdash; (Not that these messages ever change much)</center>"
    47	    echo '<center>'
    48	    echo 'Click <a href="run-dmesg-sh-list.html">here</a>'
    49	    echo 'to see the source listing of the shell script that'
    50	    echo 'creates this page, which should be run periodically'
    51	    echo 'as a cron task.<br />'
    52	    echo 'For a PHP version of this, which updates more'
    53	    echo 'regularly and is fancier too, take a look at'
    54	    echo '<a href="run-dmesg.php">run-dmesg.php</a>'
    55	    echo '</center>'
    56	else
    57	    echo ''
    58	fi
    59	
    60	echo "${BEGINH3}${MY_DATE}${ENDH3}"
    61	echo ''
    62	if [ $DO_HTML ] ; then
    63	    echo '<pre>'
    64	fi
    65	
    66	dmesg | tail -$NLINES
    67	
    68	if [ $DO_HTML ] ; then
    69	    echo '</pre>'
    70	    echo '<hr />'
    71	    echo '<center>'
    72	    echo 'For a more interesting page, with messages that'
    73	    echo 'actually change from time to time, take a look'
    74	    echo 'at the <a href="web-log.html">web activity log</a>'
    75	    echo 'page instead.<br />'
    76	    echo "And there's a fancy-schmancy PHP version of that too at"
    77	    echo '<a href="run-weblog.php">run-weblog.php</a>'
    78	    echo '</center>'
    79	    echo '<hr />'
    80	    echo '</body>'
    81	    echo '</html>'
    82	else
    83	    echo ''
    84	    echo ====================================================================== 
    85	fi
    86	
    87	
    88	# End of this script --------------------------------------------------
    89