Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Shell script to mimic journalctl -b for syslog users
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Goverp
l33t
l33t


Joined: 07 Mar 2007
Posts: 691

PostPosted: Thu Aug 01, 2019 10:47 pm    Post subject: Shell script to mimic journalctl -b for syslog users Reply with quote

For those that choose to use OpenRc syslog/syslog-ng and so forth (like me) instead of systemd and journalctl, the following shell script can be applied to a syslog file to mimic one of journalctl's useful features.
"journalctl -b" shows the log records for just one boot. (See the journalctl man page for details.)

Syntax:
Code:
bootLog <n> <syslog file>


where "n" is an optional offset; if positive, from the start of the syslog file, if negative or 0, from the end of the file. The default is -0, i.e. the most recent boot.
and "syslog file" is the name of your syslog, "/var/log/messages" by default.
Boots are identified by a string in the first line of kernel output, "kernel: Linux version"in my case. You can edit the value "bootIdentifier" in the script below.

The shell script uses awk and perhaps tac, and simply produces output to stdout. You can of course pipe it to less, for example:
Code:
bootLog -1 | less

displays the log for the previous boot.

There's no error checking, and if the offset takes you out of range of the file, you get an empty result.

Store the script below as "/usr/local/bin/bootLog" and
Code:
chmod +x /usr/local/bin/bootLog
. There's probably some smart way to get the syntax colouring in the display below, but I don't know it.
Code:
#! /bin/sh

bootIdentifier="/kernel: Linux version/"
logfile="${2:-/var/log/messages}"
which="${1:-0}"

if [ $which -le 0 ]
then
        which=$(( -$which ))
        tac $logfile | awk -v n=$which "(l==n){print} ${bootIdentifier}{l++} (l>n){exit}" | tac
else
        awk -v n=$which "${bootIdentifier}{l++} (l==n){print} (l>n){exit}" $logfile
fi

_________________
Greybeard
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum