systemctl package - github.com/taigrr/systemctl - Go Packages

This section is empty.

View Source

var (
	
	
	ErrBusFailure = errors.New("bus connection failure")
	
	ErrDoesNotExist = errors.New("unit does not exist")
	
	ErrExecTimeout = errors.New("command timed out")
	
	
	
	ErrInsufficientPermissions = errors.New("insufficient permissions")
	
	ErrLinked = errors.New("unit file linked")
	
	
	ErrMasked = errors.New("unit masked")
	
	ErrNotInstalled = errors.New("systemctl not in $PATH")
	
	
	ErrUnitNotActive = errors.New("unit not active")
	
	
	ErrUnitNotLoaded = errors.New("unit not loaded")
	
	
	ErrValueNotSet = errors.New("value not set")

	
	
	ErrUnspecified = errors.New("unknown error, please submit an issue at github.com/taigrr/systemctl")
)

View Source

var UnitTypes = []string{
	"automount",
	"device",
	"mount",
	"path",
	"scope",
	"service",
	"slice",
	"snapshot",
	"socket",
	"swap",
	"target",
	"timer",
}

UnitTypes contains all valid systemd unit type suffixes.

Reload systemd manager configuration.

This will rerun all generators (see systemd. generator(7)), reload all unit files, and recreate the entire dependency tree. While the daemon is being reloaded, all sockets systemd listens on behalf of user configuration will stay accessible.

Disables one or more units.

This removes all symlinks to the unit files backing the specified units from the unit configuration directory, and hence undoes any changes made by enable or link.

Enable one or more units or unit instances.

This will create a set of symlinks, as encoded in the [Install] sections of the indicated unit files. After the symlinks have been created, the system manager configuration is reloaded (in a way equivalent to daemon-reload), in order to ensure the changes are taken into account immediately.

unit := "syncthing"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

err := Enable(ctx, unit, Options{UserMode: true})
switch {
case errors.Is(err, ErrMasked):
	fmt.Printf("%s is masked, unmask it before enabling\n", unit)
case errors.Is(err, ErrDoesNotExist):
	fmt.Printf("%s does not exist\n", unit)
case errors.Is(err, ErrInsufficientPermissions):
	fmt.Printf("permission to enable %s denied\n", unit)
case errors.Is(err, ErrBusFailure):
	fmt.Printf("Cannot communicate with the bus\n")
case err == nil:
	fmt.Printf("%s enabled successfully\n", unit)
default:
	fmt.Printf("Error: %v", err)
}

GetMaskedUnits returns a list of all masked unit names.

Get current memory in bytes (`systemctl show [unit] --property MemoryCurrent`) as an int

Get the number of times a process restarted (`systemctl show [unit] --property NRestarts`) as an int

Get the PID of the main process (`systemctl show [unit] --property MainPID`) as an int

GetSocketsForServiceUnit returns the socket units associated with a given service unit.

Get start time of a service (`systemctl show [unit] --property ExecMainStartTimestamp`) as a `Time` type

HasValidUnitSuffix checks whether the given unit name ends with a valid systemd unit type suffix (e.g. ".service", ".timer").

Check whether any of the specified units are active (i.e. running).

Returns true if the unit is active, false if inactive or failed. Also returns false in an error case.

Check whether any of the specified units are in a "failed" state.

IsMasked checks if a unit is masked.

IsSystemd checks if systemd is the current init system by reading /proc/1/comm.

Mask one or more units, as specified on the command line. This will link these unit files to /dev/null, making it impossible to start them.

Notably, Mask may return ErrDoesNotExist if a unit doesn't exist, but it will continue masking anyway. Calling Mask on a non-existing masked unit does not return an error. Similarly, see Unmask.

Reenables one or more units.

This removes all symlinks to the unit files backing the specified units from the unit configuration directory, then recreates the symlink to the unit again, atomically. Can be used to change the symlink target.

Stop and then start one or more units specified on the command line. If the units are not running yet, they will be started.

Show a selected property of a unit. Accepted properties are predefined in the properties subpackage to guarantee properties are valid and assist code-completion.

Start (activate) a given unit

Get back the status string which would be returned by running `systemctl status [unit]`.

Generally, it makes more sense to programmatically retrieve the properties using Show, but this command is provided for the sake of completeness

Stop (deactivate) a given unit

Unmask one or more unit files, as specified on the command line. This will undo the effect of Mask.

In line with systemd, Unmask will return ErrDoesNotExist if the unit doesn't exist, but only if it's not already masked. If the unit doesn't exist but it's masked anyway, no error will be returned. Gross, I know. Take it up with Poettering.

type Options struct {
	UserMode bool
}

GetUnits returns a list of all loaded units and their states.