Files
cairnobs/internal/units/systemd.go
T
jcoffey-dev 78ab471dab units: Windows services from the Service Control Manager
Asked for and reported as units (W32Time.service), so obs-beacond judges
them as it does systemd's: running is active, stopped and set to start by
itself (or stopped with an error) has failed. Running lists them for
adopting, without per-user instances.
2026-11-14 21:42:10 -07:00

38 lines
1.1 KiB
Go

//go:build !windows
package units
import (
"context"
"fmt"
"os/exec"
"github.com/cairnobs/cairnobs/internal/report"
)
// Read asks systemd about the named units, in one call.
func Read(ctx context.Context, names []string) ([]report.Unit, error) {
if len(names) == 0 {
return nil, nil
}
args := []string{"show", "--timestamp=unix", "-p", "Id,LoadState,ActiveState,SubState,NRestarts,ActiveEnterTimestamp"}
for _, n := range names {
args = append(args, Name(n))
}
out, err := exec.CommandContext(ctx, "systemctl", args...).Output()
if err != nil {
return nil, fmt.Errorf("systemctl show: %w", err)
}
return parse(out), nil
}
// Running lists the services running now, by name, leaving out the
// per-user and per-session instances that come and go with logins.
func Running(ctx context.Context) ([]string, error) {
out, err := exec.CommandContext(ctx, "systemctl", "list-units", "--type=service", "--state=running", "--no-legend", "--plain", "--no-pager").Output()
if err != nil {
return nil, fmt.Errorf("systemctl list-units: %w", err)
}
return parseRunning(out), nil
}