cpu
Read current CPU usage and system uptime.
import { cpu } from "system";Availability
Available in the Main script only.
Table of Contents
Methods
Returns the current total CPU usage across all cores as a percentage. Calculated from the delta between consecutive calls using GetSystemTimes.
First call always returns 0
The usage calculation requires two measurements to compute a delta. The very first call to cpu.usage() always returns 0.0 because no baseline has been established yet. Subsequent calls return the actual usage measured since the previous call.
number CPU usage as a floating-point percentage in the range 0.0–100.0. Returns 0.0 if stats are unavailable or on the very first call.import { cpu } from "system";
// First call establishes the baseline — returns 0
cpu.usage();
// Poll every second for real readings
setInterval(() => {
const usage = cpu.usage();
ipcMain.send("cpu-update", { cpu: Math.round(usage) });
}, 1000);Returns system uptime since the last boot. Without a format string the raw value is a floating-point number of seconds. With a format string the tokens are replaced with the corresponding time components.
Format tokens:
| Token | Description | Example |
|---|---|---|
%d | Days (not zero-padded) | 3 |
%h | Hours within the day (not zero-padded) | 4 |
%m | Minutes within the hour (not zero-padded) | 9 |
%s | Seconds within the minute (not zero-padded) | 7 |
%H | Hours, zero-padded to 2 digits | 04 |
%M | Minutes, zero-padded to 2 digits | 09 |
%S | Seconds, zero-padded to 2 digits | 07 |
Default format
When getUpTime is called with an empty string "", the default format "%d days, %h hours, %m minutes" is used.
PARAMETERS
number | string | null Total uptime in seconds as a number when no format is given, a formatted string when a format is provided, or null if uptime cannot be read.import { cpu } from "system";
// Raw seconds (floating point)
const seconds = cpu.getUpTime();
console.log("Uptime seconds:", seconds); // e.g. 259331.234
// Formatted string
const uptime = cpu.getUpTime("%d days, %H:%M:%S");
console.log("Uptime:", uptime); // e.g. "3 days, 04:02:11"
// Using unpadded tokens
const brief = cpu.getUpTime("%d d %h h %m m");
console.log(brief); // e.g. "3 d 4 h 2 m"
// Default format (pass empty string)
const def = cpu.getUpTime("");
console.log(def); // e.g. "3 days, 4 hours, 2 minutes"Practical Examples
Live CPU bar updated every second
import { cpu } from "system";
// Warm up the baseline on startup
cpu.usage();
setInterval(() => {
const usage = cpu.usage();
ipcMain.send("stats", { cpu: Math.round(usage) });
}, 1000);Display uptime in the widget
import { cpu } from "system";
setInterval(() => {
const uptime = cpu.getUpTime("%d days, %H:%M:%S");
ipcMain.send("uptime-update", { text: uptime });
}, 1000);CPU usage with history graph
import { cpu } from "system";
const history = [];
const MAX_POINTS = 60;
// Establish baseline
cpu.usage();
setInterval(() => {
const usage = cpu.usage();
history.push(Math.round(usage));
if (history.length > MAX_POINTS) history.shift();
ipcMain.send("cpu-history", { usage, history });
}, 1000);