Categories
SysOps

How to display boot times and uptime using systemd journal

Display when the operating system was booted and calculate uptime using the systemd journal.

Notice, I am using the GNU Project’s implementation of the AWK programming language.

$ journalctl -o export | gawk '
BEGIN { 
  RS = "\n\n"
  FS = "\n"
 
  True  = 1
  False = 0
 
  monotonic_timestamp = "" 
  boot_id_prev        = ""
  first_entry         = True
} 

{
  for(field = 1; field <= NF; field++) {
    separator_index = index($field, "=")
    key             = substr($field, 0, separator_index - 1)
    value           = substr($field,    separator_index + 1)
        
    switch(key) {
      case "__REALTIME_TIMESTAMP":
        realtime_timestamp = value
        break    
      case "_BOOT_ID":
        boot_id = value
        break
      case "__MONOTONIC_TIMESTAMP":
        monotonic_timestamp = value
        break        
    }	
  }

  if(boot_id_prev != boot_id && monotonic_prev != "") {
    if(first_entry == False) {
      printf "and was up for %03d days %02d hours %02d minutes %02d seconds\n", 
             monotonic_prev/1e6/60/60/24,
             monotonic_prev/1e6/60/60%24,
             monotonic_prev/1e6/60%60,
             monotonic_prev/1e6%60

    } else {
      first_entry = False
    }
    printf "System was booted at %s ", 
           strftime("%a %b %e %H:%M:%S %Z %Y", realtime_timestamp/1e6)
  }    
  monotonic_prev = monotonic_timestamp
  boot_id_prev   = boot_id
}
END { print "and it is still running" }'

Sample output.

System was booted at Sun May 23 23:22:25 CEST 2021 and was up for 000 days 00 hours 05 minutes 20 seconds
System was booted at Sun May 23 23:27:57 CEST 2021 and was up for 018 days 11 hours 29 minutes 08 seconds
System was booted at Fri Jun 11 10:57:58 CEST 2021 and was up for 006 days 22 hours 04 minutes 26 seconds
System was booted at Sat Jun 19 10:44:45 CEST 2021 and was up for 000 days 00 hours 12 minutes 29 seconds
System was booted at Sat Jun 19 10:57:21 CEST 2021 and was up for 005 days 12 hours 28 minutes 45 seconds
System was booted at Thu Jun 24 23:26:14 CEST 2021 and was up for 006 days 13 hours 46 minutes 30 seconds
System was booted at Fri Jul  2 15:27:18 CEST 2021 and was up for 000 days 01 hours 05 minutes 21 seconds
System was booted at Fri Jul  2 16:32:50 CEST 2021 and was up for 000 days 00 hours 04 minutes 28 seconds
System was booted at Fri Jul  2 16:37:33 CEST 2021 and was up for 000 days 00 hours 02 minutes 44 seconds
System was booted at Fri Jul  2 16:40:35 CEST 2021 and was up for 000 days 00 hours 06 minutes 46 seconds
System was booted at Fri Jul  2 16:49:33 CEST 2021 and was up for 000 days 00 hours 04 minutes 25 seconds
System was booted at Fri Jul  2 16:56:05 CEST 2021 and was up for 000 days 00 hours 06 minutes 54 seconds
System was booted at Fri Jul  2 17:03:07 CEST 2021 and was up for 014 days 16 hours 42 minutes 49 seconds
System was booted at Sun Jul 18 14:11:49 CEST 2021 and was up for 000 days 00 hours 04 minutes 19 seconds
System was booted at Sun Jul 18 14:16:24 CEST 2021 and was up for 011 days 03 hours 36 minutes 17 seconds
System was booted at Thu Jul 29 17:57:56 CEST 2021 and was up for 034 days 09 hours 47 minutes 03 seconds
System was booted at Mon Sep  6 21:37:43 CEST 2021 and was up for 000 days 20 hours 49 minutes 08 seconds
System was booted at Tue Sep  7 20:24:34 CEST 2021 and was up for 000 days 17 hours 52 minutes 39 seconds
System was booted at Thu Sep  9 00:17:57 CEST 2021 and was up for 001 days 13 hours 26 minutes 10 seconds
System was booted at Fri Sep 10 23:59:27 CEST 2021 and was up for 007 days 01 hours 26 minutes 00 seconds
System was booted at Sat Sep 18 00:33:20 CEST 2021 and it is still running

This solution is using systemd journal to extract boot times and uptime, so it can be truncated.

You can compare it with wtmp database.

$ last --system shutdown reboot                                                                   
reboot   system boot  5.11.0-34-generi Sat Sep 18 00:33   still running 
shutdown system down  5.11.0-34-generi Sat Sep 18 00:32 - 00:33  (00:00)
reboot   system boot  5.11.0-34-generi Fri Sep 10 23:59 - 00:32 (7+00:33)
shutdown system down  5.11.0-34-generi Fri Sep 10 13:30 - 23:59  (10:29)
reboot   system boot  5.11.0-34-generi Thu Sep  9 00:17 - 13:30 (1+13:12)
reboot   system boot  5.11.0-31-generi Tue Sep  7 20:24 - 13:30 (2+17:05)
reboot   system boot  5.11.0-31-generi Mon Sep  6 21:37 - 13:30 (3+15:52)
reboot   system boot  5.11.0-25-generi Thu Jul 29 17:57 - 13:30 (42+19:32)
reboot   system boot  5.11.0-22-generi Sun Jul 18 14:16 - 13:30 (53+23:14)
shutdown system down  5.11.0-22-generi Sun Jul 18 14:15 - 14:16  (00:00)
reboot   system boot  5.11.0-22-generi Sun Jul 18 14:11 - 14:15  (00:04)
reboot   system boot  5.11.0-22-generi Fri Jul  2 17:03 - 14:15 (15+21:12)
shutdown system down  5.11.0-22-generi Fri Jul  2 17:02 - 17:03  (00:00)
reboot   system boot  5.11.0-22-generi Fri Jul  2 16:56 - 17:02  (00:06)
shutdown system down  5.11.0-22-generi Fri Jul  2 16:53 - 16:56  (00:02)
reboot   system boot  5.11.0-22-generi Fri Jul  2 16:49 - 16:53  (00:03)
shutdown system down  5.11.0-22-generi Fri Jul  2 16:47 - 16:49  (00:02) 
reboot   system boot  5.11.0-22-generi Fri Jul  2 16:40 - 16:47  (00:06)
shutdown system down  5.11.0-22-generi Fri Jul  2 16:40 - 16:40  (00:00)
reboot   system boot  5.11.0-22-generi Fri Jul  2 16:37 - 16:40  (00:02)
shutdown system down  5.11.0-22-generi Fri Jul  2 16:37 - 16:37  (00:00) 
reboot   system boot  5.11.0-22-generi Fri Jul  2 16:32 - 16:37  (00:04)
shutdown system down  5.8.0-59-generic Fri Jul  2 16:32 - 16:32  (00:00) 
reboot   system boot  5.8.0-59-generic Fri Jul  2 15:27 - 16:32  (01:05)
shutdown system down  5.8.0-59-generic Fri Jul  2 15:26 - 15:27  (00:00)
reboot   system boot  5.8.0-59-generic Thu Jun 24 23:26 - 15:26 (7+16:00)
shutdown system down  5.8.0-55-generic Thu Jun 24 23:25 - 23:26  (00:00)
reboot   system boot  5.8.0-55-generic Sat Jun 19 10:57 - 23:25 (5+12:28)
shutdown system down  5.8.0-55-generic Sat Jun 19 10:56 - 10:57  (00:00)
reboot   system boot  5.8.0-55-generic Sat Jun 19 10:44 - 10:56  (00:12)
shutdown system down  5.8.0-55-generic Sat Jun 19 10:42 - 10:44  (00:01)
reboot   system boot  5.8.0-55-generic Fri Jun 11 10:57 - 10:42 (7+23:44)
shutdown system down  5.8.0-53-generic Fri Jun 11 10:56 - 10:57  (00:01)
reboot   system boot  5.8.0-53-generic Sun May 23 23:27 - 10:56 (18+11:28)
shutdown system down  5.8.0-53-generic Sun May 23 23:27 - 23:27  (00:00)
reboot   system boot  5.8.0-53-generic Sun May 23 23:22 - 23:27  (00:05)
shutdown system down  5.8.0-50-generic Sun May 23 23:21 - 23:22  (00:00)
reboot   system boot  5.8.0-50-generic Sun Apr 18 17:59 - 23:21 (35+05:22)
reboot   system boot  5.8.0-48-generic Mon Apr  5 21:10 - 23:21 (48+02:11)
shutdown system down  5.8.0-48-generic Mon Apr  5 21:09 - 21:10  (00:00)
reboot   system boot  5.8.0-48-generic Mon Apr  5 21:04 - 21:09  (00:05)
reboot   system boot  5.8.0-45-generic Sun Mar 21 20:57 - 21:09 (14+23:12)
shutdown system down  5.8.0-44-generic Sat Mar 20 10:09 - 20:57 (1+10:47)
reboot   system boot  5.8.0-44-generic Thu Mar  4 01:06 - 10:09 (16+09:03)
reboot   system boot  5.8.0-44-generic Sun Feb 28 16:30 - 10:09 (19+17:39)
shutdown system down  5.8.0-44-generic Sat Feb 27 11:11 - 16:30 (1+05:18)
reboot   system boot  5.8.0-44-generic Thu Feb 25 14:31 - 11:11 (1+20:40)
reboot   system boot  5.8.0-43-generic Sun Feb 21 21:27 - 11:11 (5+13:44)
shutdown system down  5.8.0-43-generic Sun Feb 21 21:27 - 21:27  (00:00)
reboot   system boot  5.8.0-43-generic Sun Feb 21 21:24 - 21:27  (00:03)
shutdown system down  5.8.0-43-generic Sun Feb 21 21:23 - 21:24  (00:00)
reboot   system boot  5.8.0-43-generic Sun Feb 21 21:19 - 21:23  (00:03)
shutdown system down  5.8.0-43-generic Sun Feb 21 21:19 - 21:19  (00:00)
reboot   system boot  5.8.0-43-generic Sun Feb 21 21:13 - 21:19  (00:05)
shutdown system down  5.8.0-43-generic Sun Feb 21 21:13 - 21:13  (00:00)
reboot   system boot  5.8.0-43-generic Sun Feb 21 21:08 - 21:13  (00:04)
shutdown system down  5.8.0-43-generic Sun Feb 21 21:08 - 21:08  (00:00)
reboot   system boot  5.8.0-43-generic Sun Feb 21 20:55 - 21:08  (00:13)
shutdown system down  5.8.0-43-generic Sun Feb 21 20:54 - 20:55  (00:00)
reboot   system boot  5.8.0-43-generic Sun Feb 21 20:51 - 20:54  (00:03)
shutdown system down  5.8.0-43-generic Sun Feb 21 20:50 - 20:51  (00:00)
reboot   system boot  5.8.0-43-generic Sun Feb 21 20:46 - 20:50  (00:04)
shutdown system down  5.8.0-43-generic Sun Feb 21 20:46 - 20:46  (00:00)
reboot   system boot  5.8.0-43-generic Sun Feb 21 20:41 - 20:46  (00:04)
shutdown system down  5.8.0-43-generic Sun Feb 21 20:41 - 20:41  (00:00)
reboot   system boot  5.8.0-43-generic Fri Feb 19 17:08 - 20:41 (2+03:33)
shutdown system down  5.8.0-43-generic Fri Feb 19 17:07 - 17:08  (00:00)
reboot   system boot  5.8.0-43-generic Fri Feb 19 17:04 - 17:07  (00:02)
shutdown system down  5.8.0-43-generic Fri Feb 19 16:03 - 17:04  (01:01)
reboot   system boot  5.8.0-43-generic Wed Feb 17 01:24 - 16:03 (2+14:39)
shutdown system down  5.8.0-43-generic Wed Feb 17 01:23 - 01:24  (00:00)
reboot   system boot  5.8.0-43-generic Wed Feb 10 01:16 - 01:23 (7+00:07)
shutdown system down  5.8.0-43-generic Wed Feb 10 01:15 - 01:16  (00:00)
reboot   system boot  5.8.0-43-generic Wed Feb 10 01:02 - 01:15  (00:12)

wtmp begins Wed Feb 10 01:02:48 2021

You will get some differences in uptime values. Play with the awk script to understand why this is happening.