Send a link

Shell scripting



Archive dmesg on reboot

This small script will log dmesg on booting and tail 100 lines of messages on shutdown.

Preparing directory
mkdir /root/dmesg-archive

Put the following in /usr/local/etc/rc.d/dmesg-archive.sh and chmod 755 it.
#!/bin/sh

DATE=`/bin/date "+%Y-%m_%H-%M-%S"`

case "\$1" in

        start)
                /sbin/dmesg > /root/dmesg-archive/dmesg_$DATE
                ;;
        stop)
                /usr/bin/tail -100 /var/log/messages > /root/dmesg-archive/messages_$DATE
                ;;
        *)
                echo ""
                echo "Usage: `basename \$0` { start | stop }"
                echo ""
                exit 1
                ;;
esac


Using dates in scripts

EPOC=`/bin/date "+%s"`
DAY=`/bin/date "+%d"`
HOUR=`/bin/date "+%H"`
MINUTE=`/bin/date "+%M"`
SECOND=`/bin/date "+%S"`
WEEKDAY=`/bin/date "+%w"`
DATETIME="D${DAY}_H${HOUR}"
WD="WD${WEEKDAY}"
FULLDATE=`/bin/date "+%Y-%m-%d"`