chenhg5/cc-connect · error

systemctl %s: %s (%w)

Error message

systemctl %s: %s (%w)

What it means

Install() wraps a failure of one of the three systemctl commands it runs: daemon-reload, enable, restart. The error message embeds the joined args, the command's stdout, and the underlying error, so it usually says exactly which systemctl step failed and why.

Source

Thrown at daemon/systemd.go:82

	// units (/etc/systemd/system/) the file is owned by root and remains
	// readable by root only; for user-level units under
	// ~/.config/systemd/user it remains owner-only. WriteFile only applies
	// perm on create, so Chmod afterwards is required to harden reinstalls
	// of pre-existing 0644 units from earlier cc-connect versions.
	if err := os.WriteFile(unitPath, []byte(unit), 0600); err != nil {
		return fmt.Errorf("write unit file: %w", err)
	}
	if err := os.Chmod(unitPath, 0600); err != nil {
		return fmt.Errorf("chmod unit file: %w", err)
	}

	for _, cmdArgs := range [][]string{
		m.sysArgs("daemon-reload"),
		m.sysArgs("enable", systemdServiceName),
		m.sysArgs("restart", systemdServiceName),
	} {
		if out, err := runSystemctl(cmdArgs...); err != nil {
			return fmt.Errorf("systemctl %s: %s (%w)", strings.Join(cmdArgs, " "), out, err)
		}
	}

	return nil
}

func (m *systemdManager) Uninstall() error {
	if _, err := runSystemctl(m.sysArgs("disable", "--now", systemdServiceName)...); err != nil {
		slog.Warn("systemd: disable failed", "error", err)
	}

	unitPath := m.unitPath()
	if err := os.Remove(unitPath); err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("remove unit: %w", err)
	}

	if _, err := runSystemctl(m.sysArgs("daemon-reload")...); err != nil {
		slog.Warn("systemd: daemon-reload failed", "error", err)

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Read the embedded stdout in the error — it names the failing step and systemctl's reason.
  2. Verify systemd is PID 1: `ps -p 1`; in WSL2 enable [boot] systemd=true in /etc/wsl.conf.
  3. Inspect the unit: `systemctl status cc-connect` and `journalctl -u cc-connect -e`.
  4. Validate the unit and service: `systemd-analyze verify <unitPath>`; fix config and reinstall.

Example fix

// before
mgr.Install(cfg) // systemctl restart cc-connect: ... (exit status 1) — bad ExecStart
// after
if err := mgr.Install(cfg); err != nil {
    slog.Error("install failed", "error", err) // inspect embedded systemctl output
    return err
}
Defensive patterns

Strategy: try-catch

Validate before calling

if err := exec.Command("systemctl", "is-system-running").Run(); err != nil {
    return fmt.Errorf("systemd not operational: %w", err)
}
if _, err := os.Stat(mgr.unitPath()); err != nil {
    return fmt.Errorf("unit not installed yet")
}

Type guard

func systemdAvailable() bool {
    return exec.Command("systemctl", "is-system-running").Run() == nil
}

Try / catch

if err := mgr.Install(cfg); err != nil {
    if strings.Contains(err.Error(), "systemctl") {
        // error message embeds args + stdout: log it verbatim for diagnosis
        slog.Error("systemctl step failed", "detail", err.Error())
    }
    return err
}

Prevention

When it happens

Trigger: systemctl is missing or the system instance is unreachable; the generated unit file is invalid and enable/restart rejects it; the service fails to start (bad ExecStart, port already in use, missing binary); running without privileges.

Common situations: WSL2 without systemd enabled (systemctl fails with 'System has not been booted with systemd'); syntax error in unit from malformed config values; cc-connect binary path moved after install; editing config.toml with a value that breaks the unit.

Understand the failure class

Background: "git command failed": what it means when a tool shells out to git and git exits non-zero — this error's family across 21 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/061b039624e03dd8. Report an issue: GitHub.