shelltask

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package shelltask runs shell commands in process groups of their own and keeps their output in a size-capped file.

Index

Constants

View Source
const (
	MaxBytes  = 20 << 20
	HeadBytes = 1 << 20
)

An output file holds at most MaxBytes: once full it keeps the first HeadBytes, a marker naming how much was dropped, and the newest output after it.

Variables

This section is empty.

Functions

func Excerpt added in v0.2.1

func Excerpt(path string, limit int) (string, bool, error)

Excerpt returns the output at path whole when it fits in limit bytes, and otherwise its head, a line naming how many bytes are left out, and its tail, which gets two thirds of the room as failures are usually reported last. Both parts end or start at a line break when one is near.

func KillGroup

func KillGroup(pgid int) error

KillGroup sends SIGKILL to the process group; a gone group is no error.

func KillLeftover

func KillLeftover(leader Leader) error

KillLeftover kills the process group a daemon that is gone started. The group is left alone when its leader is gone, is another process that got its PID, or cannot be checked; its processes then outlive the task.

func LoginEnvironment added in v0.2.1

func LoginEnvironment(environ []string, timeout time.Duration) ([]string, error)

LoginEnvironment is the environment a bash login shell started with environ sets up, such as the PATH entries of profile scripts; read once, it lets commands run with a plain bash -c, which is faster and prints nothing extra.

func Tail

func Tail(path string, limit int) (string, error)

Tail returns the last limit bytes of the output at path.

Types

type Chunk

type Chunk struct {
	Text string
	// Next is the cursor after Text; Skipped counts bytes dropped before it.
	Next    int64
	Skipped int64
	// More is set when output past Next was already written.
	More bool
}

Chunk is output read from a cursor on.

func Read

func Read(path string, cursor int64, limit int) (Chunk, error)

Read returns up to limit bytes of the output at path from cursor on, an output offset that survives dropping; it never splits a UTF-8 character.

type Leader

type Leader struct {
	PID    int
	BootID string
	Start  string
}

Leader identifies a process group's leader across daemon restarts: the boot it ran in and its start within it tell it apart from a later process with its PID. Empty fields could not be read.

type Output

type Output struct {
	// contains filtered or unexported fields
}

Output is the file a command writes its stdout and stderr to.

func CreateOutput

func CreateOutput(path string) (*Output, error)

CreateOutput creates the file at path, readable by the owner only.

func (*Output) Close

func (o *Output) Close() error

Close closes the file; the output stays readable.

func (*Output) Path

func (o *Output) Path() string

Path is where the output is kept.

func (*Output) Write

func (o *Output) Write(p []byte) (int, error)

Write appends p; output past MaxBytes drops the oldest bytes after the head.

type Process

type Process struct {
	// contains filtered or unexported fields
}

Process is a running shell command; its process group has the ID of its PID.

func Start

func Start(command, dir string, env []string, box sandbox.Policy, out *Output) (*Process, error)

Start runs command with bash -c in dir and env (nil inherits the daemon's) under box, a resolved sandbox policy, in a process group of its own and with stdin from /dev/null, its stdout and stderr going to out until every process that has them, the shell and the commands it left running, closed them.

func (*Process) Done

func (p *Process) Done() <-chan struct{}

Done is closed once the process exited and its output is closed.

func (*Process) ExitCode

func (p *Process) ExitCode() int

ExitCode is the shell's exit code once Done is closed; -1 when a signal ended it.

func (*Process) Exited

func (p *Process) Exited() <-chan struct{}

Exited is closed once the shell exited; commands it left running may still write.

func (*Process) KillLeftovers added in v0.2.1

func (p *Process) KillLeftovers() bool

KillLeftovers kills the processes the shell left running in its group once it exited, and reports whether there were any.

func (*Process) Leader

func (p *Process) Leader() Leader

Leader identifies the shell, the leader of the process group.

func (*Process) Output

func (p *Process) Output() *Output

Output is where the process writes.

func (*Process) PID

func (p *Process) PID() int

PID is the shell's process ID, which is also its process group ID.

func (*Process) StartedAt

func (p *Process) StartedAt() time.Time

StartedAt is when the process was started.

func (*Process) Stop

func (p *Process) Stop(grace time.Duration) error

Stop asks every process of the group to end with SIGTERM and kills the group with SIGKILL when the command is not done within grace.

func (*Process) WaitOutput

func (p *Process) WaitOutput(delay time.Duration)

WaitOutput waits for the shell to exit and then at most delay for the commands it left running to close the output; after that it stops reading what they write. It returns once Done is closed.

type Ran added in v0.2.1

type Ran struct {
	Stdout   []byte
	Stderr   []byte
	ExitCode int
}

Ran is what a command Run waited for printed, cut at its limit, and its exit code.

func Run added in v0.2.1

func Run(ctx context.Context, command, dir string, env []string, stdin []byte, limit int) (Ran, error)

Run runs command with bash -c in dir and env (nil inherits the daemon's), in a process group of its own, with stdin as its input, and waits for it. When ctx stops first its group is killed and ctx's error returned. Processes it leaves in its group are killed; each output keeps at most limit bytes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL