exec

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2024 License: Unlicense Imports: 5 Imported by: 1

README

exec Package

Go Reference Go Report Card Tests

The exec package in Go is designed to run external commands. It is similar to the os/exec package, but provides a simpler interface and does not support streaming.

Main Features

  • The main difference between exec and os/exec is that the process does not wait for child processes to finish. This is useful for running long-running processes that are expected to run in the background.

Example

out, err := exec.Command("date").Output()
if err != nil {
  log.Fatal(err)
}
fmt.Printf("The date is %s\n", out)

This example runs the date command and logs any errors that occur.

Documentation

Overview

The main difference is that the process does not wait for child processes to finish. This is useful for running long-running processes that are expected to run in the background.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	// Path is the path of the command to run.
	//
	// This is the only field that must be set to a non-zero
	// value. If Path is relative, it is evaluated relative
	// to Dir.
	Path string

	// Args holds command line arguments, including the command as Args[0].
	// If the Args field is empty or nil, Run uses {Path}.
	//
	// In typical use, both Path and Args are set by calling Command.
	Args []string

	// Process is the underlying process, once started.
	Process *os.Process

	// ProcessState contains information about an exited process.
	// If the process was started successfully, Wait or Run will
	// populate its ProcessState when the command completes.
	ProcessState *os.ProcessState
	// contains filtered or unexported fields
}

Cmd represents an external command being prepared or run.

A Cmd cannot be reused after calling its Output or CombinedOutput methods.

func Command

func Command(name string, arg ...string) *Cmd

Command returns the Cmd struct to execute the named program with the given arguments.

It sets only the Path and Args in the returned structure.

func (*Cmd) CombinedOutput

func (c *Cmd) CombinedOutput() ([]byte, error)

CombinedOutput runs the command and returns its combined standard output and standard error.

Example
package main

import (
	"fmt"
	"log"

	"github.com/mrmarble/exec"
)

func main() {
	out, err := exec.Command("/bin/sh", "-c", "echo stdout; echo 1>&2 stderr").CombinedOutput()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Output: %s\n", out)
}
Output:

Output: stdout
stderr

func (*Cmd) Output

func (c *Cmd) Output() ([]byte, error)

Output runs the command and returns its standard output.

Example
package main

import (
	"fmt"
	"log"

	"github.com/mrmarble/exec"
)

func main() {
	out, err := exec.Command("date").Output()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("The date is %s\n", out)
}

Jump to

Keyboard shortcuts

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