Converting dmesg Timestamps with Busybox
Server
Published: 2023-02-24

The Busybox dmesg command lacks the -T option to convert and display timestamps, so I tried converting them with a one-liner.

One-liner

uptime=$(cut -d. -f1 /proc/uptime); dmesg | while read line; do time=$(echo $line | sed -e 's/^\[ *\([0-9]*\).*$/\1/g');  echo "[$(date -d @$(($(date +%s) - $uptime + $time)))] $(echo $line | cut -d ] -f 2 -)" ; done

Brief Explanation

  1. /proc/uptime records the elapsed time since the system was started in seconds.
  2. The timestamps output by dmesg are the elapsed time since the system was started.
  3. By subtracting the elapsed time since the system was started from the current time, you can determine the system’s start time.
Current time - (Total uptime - dmesg timestamp) = Log time