shelltask

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 13 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 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 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, out *Output) (*Process, error)

Start runs command with bash -lc in dir, in a process group of its own, 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) 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.

Jump to

Keyboard shortcuts

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