I would like to filter by time. for instance:
dmesg -sec 30
The output would be events that happened for last 30 seconds.
Or with time interval. Any filtering with time.
Asked
Active
Viewed 5,294 times
3 Answers
1
You should be able to do it using:
journalctl -kS -25min
-k is for kernel messages only and -S to define time.
For opposite direction, more verbose output limited to 1000 lines, maybe try: -kexS -25min
also check man systemd.time

16851556
- 561
1
Write a script to add the current time to timestamp in the dmegs entry, then compare the time to the current time, and display the lines that are more recent than the desired interval.
This is an example:
#!/bin/bash
if [ $# -eq 0 ]
then
echo "Argument Error. Please specify the Time interval in seconds."
exit
fi
timeinterval=$1 # change this to the desired time interval
currenttime=$(date +%s)
string="$(who -b | awk '{print $3"-"$4}')"
IFS='-' read -a array <<< "$string"
MONTHS=(X Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
year="${array[0]}"
month=${MONTHS[${array[1]}]}
day="${array[2]}"
hour="${array[3]}"
daystring="$month $day $year $hour"
bootime=$(date --date="$daystring" +%s)
dmesg | while read line; do
dmesgstamp=$(echo "$line" | awk -F'[' '{print $2}' | awk -F\. '{print $1}')
dmesgstamp=$((dmesgstamp += bootime))
results=$((currenttime-dmesgstamp))
if ((results < timeinterval)); then
echo "$line"
fi
done
Run the script with the interval in seconds as the argument:
$ ./script.sh 30

L. D. James
- 25,036
0
Attention please, the answer from L. D. James has to be modified if LC_LOCALE=POSIX which will lead to an error because the "year" is missing:
linuxmaster:~ # who -b
system boot Oct 11 13:44
Better (in my example german LC_LOCALE works):
string="$(LC_TIME="de_DE.UTF-8" who -b | awk '{print $3"-"$4}')"
-
Please provide some clarification why my addition is not helpful. (Downvote?) – Syntaxrabbit Feb 08 '22 at 00:16