timeout

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Overview

Package timeout provides a GNU-compatible timeout command.

Index

Examples

Constants

View Source
const (
	// ExitSuccess means the command completed successfully.
	ExitSuccess = 0
	// ExitTimedOut means the command timed out.
	ExitTimedOut = 124
	// ExitInternalFailure means timeout itself failed.
	ExitInternalFailure = 125
	// ExitCannotInvoke means the command was found but could not run.
	ExitCannotInvoke = 126
	// ExitNotFound means the command was not found.
	ExitNotFound = 127
)

Variables

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

ErrUsage identifies an invalid command-line argument.

Functions

func HelpText

func HelpText() string

HelpText returns the help text for the timeout command.

func ParseDuration

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

ParseDuration reads a duration in GNU timeout format.

func ParseSignal

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

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

func Run

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

Run runs timeout with the given arguments and streams. It returns the command's exit code or a timeout exit code.

Example
package main

import (
	"bytes"
	"fmt"
	"strings"

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

func main() {
	cmdArgs := []string{
		"-k", "1s", "-s", "TERM", "2s",
		"go", "version",
	}

	stdout := new(bytes.Buffer)
	stderr := new(bytes.Buffer)

	exitCode := timeout.Run(cmdArgs, timeout.Streams{
		Stdin:  bytes.NewReader(nil),
		Stdout: stdout,
		Stderr: stderr,
	})

	output := strings.Join(strings.Fields(stdout.String())[0:2], " ")

	fmt.Println("exit code:", exitCode)
	fmt.Println(strings.TrimSpace("stderr: " + stderr.String()))
	fmt.Println(strings.TrimSpace("stdout: " + output))
}
Output:
exit code: 0
stderr:
stdout: go version
Example (Help)
package main

import (
	"bytes"
	"fmt"
	"strings"

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

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

	exitCode := timeout.Run([]string{"--help"}, timeout.Streams{
		Stdin:  bytes.NewReader(nil),
		Stdout: stdout,
		Stderr: stderr,
	})

	fmt.Println("exit code:", exitCode)
	fmt.Println(strings.TrimSpace("stderr: " + stderr.String()))
	fmt.Println(strings.TrimSpace("stdout: " + stdout.String()[:6]))
}
Output:
exit code: 0
stderr:
stdout: Usage:

Types

type Config

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

Config contains the parsed options, duration, and command.

func Parse

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

Parse reads timeout command-line arguments and returns their configuration.

Example
package main

import (
	"fmt"

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

func main() {
	cmdArgs := []string{
		"-k", "1s", "-s", "TERM", "2s",
		"printf", "ok",
	}

	config, err := timeout.Parse(cmdArgs)

	fmt.Println("error:", err)
	fmt.Println("kill after:", config.KillAfter)
	fmt.Println("duration:", config.Duration)
	fmt.Println("command:", config.Command)
}
Output:
error: <nil>
kill after: 1s
duration: 2s
command: [printf ok]

type Streams

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

Streams contains the input and output streams used by Run.

Jump to

Keyboard shortcuts

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