timeout

package
v1.0.0-alpha Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package timeout implements a GNU-compatible timeout command.

Index

Examples

Constants

View Source
const (
	// ExitSuccess is the exit code used for successful execution.
	ExitSuccess = 0
	// ExitTimedOut is the exit code used when the command times out.
	ExitTimedOut = 124
	// ExitInternalFailure is the exit code used for timeout's own failures.
	ExitInternalFailure = 125
	// ExitCannotInvoke is the exit code used when a command cannot be invoked.
	ExitCannotInvoke = 126
	// ExitNotFound is the exit code used when a command cannot be found.
	ExitNotFound = 127
)

Variables

View Source
var ErrUsage = errors.New("usage error")

ErrUsage marks command-line usage errors.

Functions

func HelpText

func HelpText() string

HelpText returns the command help text.

func ParseDuration

func ParseDuration(value string) (time.Duration, error)

ParseDuration parses GNU timeout duration syntax.

func ParseSignal

func ParseSignal(value string) (syscall.Signal, error)

ParseSignal parses a signal name, SIG-prefixed name, or signal number.

func Run

func Run(args []string, streams Streams) int

Run runs timeout with the given arguments and streams and returns an exit code.

Example (Help)
package main

import (
	"bytes"
	"fmt"

	"github.com/KEINOS/go-timeout/timeout"
)

func main() {
	stdout := new(bytes.Buffer)
	stderr := new(bytes.Buffer)

	code := timeout.Run([]string{"--help"}, timeout.Streams{
		Stdout: stdout,
		Stderr: stderr,
	})

	fmt.Println(code)
	fmt.Println(stderr.String())
	fmt.Println(stdout.String()[:6])
}
Output:
0

Usage:

Types

type Config

type Config struct {
	Foreground     bool
	PreserveStatus bool
	Verbose        bool
	ShowHelp       bool
	ShowVersion    bool
	Duration       time.Duration
	KillAfter      time.Duration
	Signal         syscall.Signal
	Command        []string
}

Config contains parsed timeout options and operands.

func Parse

func Parse(args []string) (Config, error)

Parse parses timeout command-line arguments.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-timeout/timeout"
)

func main() {
	config, err := timeout.Parse([]string{"-k", "1s", "-s", "TERM", "2s", "printf", "ok"})

	fmt.Println(err)
	fmt.Println(config.KillAfter)
	fmt.Println(config.Duration)
	fmt.Println(config.Command)
}
Output:
<nil>
1s
2s
[printf ok]

type Streams

type Streams struct {
	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
}

Streams contains standard streams used by Run.

Jump to

Keyboard shortcuts

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