12 May 2024 ~ 1 min read

How to calculate uptime?


Uptime

how to calculate uptime in days or hours?

  1. get last reboot timestamp with
lastRebootTimestamp=$(sysctl kern.boottime | awk -F'[= |,]' '{print $6}')
  1. get now timestamp with
nowTimestamp=$(date +%s)
  1. calculate the difference
upTimestamp=$((nowTimestamp-lastRebootTimestamp))
  1. calculate days
uptimeDays=$((upTimestamp/(24*60*60)))
  1. calculate hours
uptimeHours=$((upTimestamp/(60*60)))

Headshot of Jurij Schlaht

Hi, I'm Jurij. I'm a web developer, macOS system administrator, photographer and soon also data engineer based in Frankfurt. You can follow me on Instagram, see some of my work on GitHub, or read more about me here on this page.