Skip to main content
The BootTime() function returns the system boot time expressed in seconds since the Unix epoch (January 1, 1970 UTC).

Function Signatures

func BootTime() (uint64, error)
func BootTimeWithContext(ctx context.Context) (uint64, error)

func Uptime() (uint64, error)
func UptimeWithContext(ctx context.Context) (uint64, error)

BootTime

Returns the timestamp when the system was last booted.

Return Value

bootTime
uint64
Unix timestamp (seconds since epoch) when the system booted
error
error
Error if boot time cannot be determined

Uptime

Returns the number of seconds since the system was booted.

Return Value

uptime
uint64
Number of seconds the system has been running
error
error
Error if uptime cannot be determined

Usage Examples

Get Boot Time

package main

import (
    "fmt"
    "time"
    "github.com/shirou/gopsutil/v4/host"
)

func main() {
    bootTime, err := host.BootTime()
    if err != nil {
        panic(err)
    }
    
    // Convert to time.Time
    bootTimeObj := time.Unix(int64(bootTime), 0)
    fmt.Printf("System booted at: %s\n", bootTimeObj.Format(time.RFC1123))
}

Get Uptime

package main

import (
    "fmt"
    "time"
    "github.com/shirou/gopsutil/v4/host"
)

func main() {
    uptime, err := host.Uptime()
    if err != nil {
        panic(err)
    }
    
    // Convert to duration
    duration := time.Duration(uptime) * time.Second
    
    days := int(duration.Hours() / 24)
    hours := int(duration.Hours()) % 24
    minutes := int(duration.Minutes()) % 60
    
    fmt.Printf("System uptime: %d days, %d hours, %d minutes\n", 
        days, hours, minutes)
}

Calculate Boot Time from Uptime

package main

import (
    "fmt"
    "time"
    "github.com/shirou/gopsutil/v4/host"
)

func main() {
    uptime, err := host.Uptime()
    if err != nil {
        panic(err)
    }
    
    now := time.Now()
    bootTime := now.Add(-time.Duration(uptime) * time.Second)
    
    fmt.Printf("System booted at: %s\n", bootTime.Format(time.RFC1123))
    fmt.Printf("Uptime: %s\n", time.Duration(uptime)*time.Second)
}

With Context and Timeout

package main

import (
    "context"
    "fmt"
    "time"
    "github.com/shirou/gopsutil/v4/host"
)

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
    defer cancel()
    
    bootTime, err := host.BootTimeWithContext(ctx)
    if err != nil {
        panic(err)
    }
    
    uptime, err := host.UptimeWithContext(ctx)
    if err != nil {
        panic(err)
    }
    
    fmt.Printf("Boot Time: %d\n", bootTime)
    fmt.Printf("Uptime: %d seconds\n", uptime)
}

Monitoring System Reboots

package main

import (
    "fmt"
    "time"
    "github.com/shirou/gopsutil/v4/host"
)

func monitorReboots() {
    initialBootTime, err := host.BootTime()
    if err != nil {
        panic(err)
    }
    
    ticker := time.NewTicker(10 * time.Second)
    defer ticker.Stop()
    
    for range ticker.C {
        currentBootTime, err := host.BootTime()
        if err != nil {
            fmt.Printf("Error checking boot time: %v\n", err)
            continue
        }
        
        if currentBootTime != initialBootTime {
            fmt.Println("System was rebooted!")
            initialBootTime = currentBootTime
        }
    }
}

Boot Time Caching

You can enable caching for boot time to improve performance when calling BootTime() frequently:
package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/host"
)

func main() {
    // Enable boot time caching
    host.EnableBootTimeCache(true)
    
    // First call reads from system
    bootTime1, _ := host.BootTime()
    
    // Subsequent calls use cached value
    bootTime2, _ := host.BootTime()
    
    fmt.Printf("Boot times match: %v\n", bootTime1 == bootTime2)
    
    // Disable caching if needed
    host.EnableBootTimeCache(false)
}
When caching is enabled, BootTime() will not detect system reboots. Only enable caching if you’re certain the system won’t reboot during your program’s execution.

Platform Implementation

Reads from /proc/stat or /proc/uptime file

Error Handling

Possible error conditions:
  • Unable to read system files (Linux)
  • System call failures
  • Permission denied errors
  • Context cancellation or timeout

Info()

Get comprehensive host information including boot time and uptime

Host Overview

Back to host package overview

Build docs developers (and LLMs) love