# CasaOS Cron Job for SendAllHardwareStatusBySocket: Real-Time Hardware Monitoring Explained

> Discover the purpose of the CasaOS Cron Job for SendAllHardwareStatusBySocket. This job collects real-time hardware metrics every 5 seconds and broadcasts them via the internal notification socket.

- Repository: [IceWhale/CasaOS](https://github.com/IceWhaleTech/CasaOS)
- Tags: deep-dive
- Published: 2026-06-28

---

**The cron job executes every 5 seconds to gather live hardware metrics and broadcast them via CasaOS' internal notification socket under the event name `casaos:system:utilization`.**

The IceWhaleTech/CasaOS repository implements a lightweight background scheduler that continuously synchronizes the web interface with current system performance data. This cron job for SendAllHardwareStatusBySocket aggregates network statistics, CPU utilization, memory usage, and thermal readings, publishing them through the internal message bus for real-time consumption by front-end clients and third-party integrations.

## What Is the SendAllHardwareStatusBySocket Cron Job?

In [`main/main.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/main/main.go), CasaOS initializes a cron scheduler that triggers every five seconds to collect hardware telemetry. The registration occurs at lines 121-123, where the service creates a new cron instance with seconds precision and adds the periodic function:

```go
c := cron.New(cron.WithSeconds())
c.AddFunc("@every 5s", route.SendAllHardwareStatusBySocket)
c.Start()

```

This configuration ensures that `route.SendAllHardwareStatusBySocket()` runs continuously while the CasaOS binary is active, acting as the aggregation engine for system-level metrics.

## How Hardware Metrics Are Collected

The implementation in [`route/periodical.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/route/periodical.go) performs a systematic multi-step collection process before broadcasting the results to subscribed clients.

### Network Interface Monitoring

The function iterates over known network interfaces to identify active connections. It records I/O counters for each interface and assigns human-readable states such as `Up` or `Down` based on current link status, providing real-time network throughput data.

### CPU Statistics and Thermal Data

CPU collection involves reading the current usage percentage, detecting the processor model (ARM, Intel, or AMD), counting physical cores, and fetching temperature and power readings from system sensors. This data is gathered through the system service layer to ensure accurate hardware detection across different architectures.

### Memory Utilization

The job pulls total and available memory statistics from the system driver via `service.MyService.Notify()`, providing immediate insight into RAM consumption and availability on the host system.

### Additional Temperature Sensors

Custom temperature entries are retrieved from the notification subsystem using `service.MyService.Notify().GetSystemTempMap()`, allowing integration with specialized hardware monitoring plugins and custom sensor configurations.

### Broadcasting via the Notification Bus

After aggregating data into a structured map containing `sys_mem`, `sys_cpu`, `sys_net`, and temperature keys, the function publishes the payload:

```go
service.MyService.Notify().SendNotify("casaos:system:utilization", body)

```

This call distributes the hardware snapshot to all subscribers through CasaOS' internal message bus, enabling real-time dashboard updates and alerting mechanisms.

## Source Code Implementation Details

The hardware monitoring pipeline relies on coordination between several key components:

- **[`main/main.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/main/main.go)**: Creates the cron scheduler and registers the 5-second interval at lines 121-123
- **[`route/periodical.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/route/periodical.go)**: Implements `SendAllHardwareStatusBySocket()` with the complete collection and broadcasting logic
- **[`service/system.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/service/system.go)**: Provides underlying system APIs such as `GetNetInfo()` and `GetCpuPercent()` that supply raw hardware data
- **[`service/notify.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/service/notify.go)**: Manages the internal message bus that handles the `casaos:system:utilization` event distribution

## Consuming Hardware Status Data

Front-end applications and third-party integrations can subscribe to the notification stream to receive real-time updates without polling the system directly.

The following JavaScript example connects to the CasaOS WebSocket endpoint and filters for hardware status events:

```javascript
const ws = new WebSocket('ws://<casaos-host>:<port>/ws');
ws.addEventListener('message', evt => {
    const data = JSON.parse(evt.data);
    if (data.event === 'casaos:system:utilization') {
        console.log('Hardware status:', data.payload);
        // Access data.payload.sys_cpu, data.payload.sys_mem, etc.
    }
});

```

For debugging or development purposes, you can manually trigger a single broadcast by importing the route package and calling the function directly:

```go
package main

import "github.com/IceWhaleTech/CasaOS/route"

func main() {
    // Force immediate hardware status broadcast
    route.SendAllHardwareStatusBySocket()
}

```

## Summary

- The cron job for SendAllHardwareStatusBySocket runs every 5 seconds via the scheduler defined in [`main/main.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/main/main.go) lines 121-123
- It collects network interface states and I/O counters, CPU usage percentages and model information, memory statistics, and temperature readings
- Data is published under the event name `casaos:system:utilization` through the internal notification bus managed by [`service/notify.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/service/notify.go)
- Client applications subscribe via WebSocket to receive real-time hardware monitoring updates without polling
- The implementation spans [`route/periodical.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/route/periodical.go), [`service/system.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/service/system.go), and [`service/notify.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/service/notify.go) to provide cross-platform hardware detection

## Frequently Asked Questions

### What interval does the SendAllHardwareStatusBySocket cron job use?

The cron job executes every 5 seconds using the `@every 5s` schedule syntax in the `cron.New(cron.WithSeconds())` scheduler initialized in [`main/main.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/main/main.go) at lines 121-123.

### Where is the SendAllHardwareStatusBySocket function implemented?

The function is implemented in [`route/periodical.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/route/periodical.go). This file contains the aggregation logic that gathers network, CPU, memory, and temperature data before publishing it to the notification system via `service.MyService.Notify().SendNotify()`.

### How can I subscribe to hardware status updates from CasaOS?

Client applications can connect to the CasaOS WebSocket endpoint at `ws://<host>:<port>/ws` and listen for messages where the `event` field equals `casaos:system:utilization`. The payload contains current system metrics including CPU utilization, memory usage, and network interface statistics.

### What hardware metrics does the cron job collect?

According to the source code in [`route/periodical.go`](https://github.com/IceWhaleTech/CasaOS/blob/main/route/periodical.go), the job collects network interface I/O counters and link status, CPU usage percentage and processor model information, total and available memory statistics, and temperature readings from both system sensors and the notification subsystem's custom temperature map.