system

package
v2.3.7 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 11 Imported by: 4

Documentation

Overview

Package system contain some functions about os, runtime, shell command.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareOsEnv

func CompareOsEnv(key, comparedEnv string) bool

CompareOsEnv gets env named by the key and compare it with comparedEnv. Play: https://go.dev/play/p/BciHrKYOHbp

Example
err := SetOsEnv("foo", "abc")
if err != nil {
	return
}

result := CompareOsEnv("foo", "abc")

fmt.Println(result)
Output:

true

func ExecCommand

func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)

ExecCommand execute command, return the stdout and stderr string of command, and error if error occur param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1 in linux, use /bin/bash -c to execute command in windows, use powershell.exe to execute command Play: https://go.dev/play/p/n-2fLyZef-4

Example
_, stderr, err := ExecCommand("ls")
// fmt.Println(stdout)
fmt.Println(stderr)
fmt.Println(err)
Output:


<nil>

func GetOsBits added in v2.1.3

func GetOsBits() int

GetOsBits return current os bits (32 or 64). Play: https://go.dev/play/p/ml-_XH3gJbW

Example
osBits := GetOsBits()

fmt.Println(osBits)
Output:

64

func GetOsEnv

func GetOsEnv(key string) string

GetOsEnv gets the value of the environment variable named by the key. Play: https://go.dev/play/p/D88OYVCyjO-

Example
ok := SetOsEnv("foo", "abc")
result := GetOsEnv("foo")

fmt.Println(ok)
fmt.Println(result)
Output:

<nil>
abc

func IsLinux

func IsLinux() bool

IsLinux check if current os is linux. Play: https://go.dev/play/p/zIflQgZNuxD

func IsMac

func IsMac() bool

IsMac check if current os is macos. Play: https://go.dev/play/p/Mg4Hjtyq7Zc

func IsWindows

func IsWindows() bool

IsWindows check if current os is windows. Play: https://go.dev/play/p/XzJULbzmf9m

func KillProcess added in v2.3.3

func KillProcess(pid int) error

KillProcess kill a process by pid. Play: https://go.dev/play/p/XKmvV-ExBWa

Example
pid, err := StartProcess("sleep", "3")
if err != nil {
	return
}
time.Sleep(1 * time.Second)

err = KillProcess(pid)

fmt.Println(err)
Output:

<nil>

func RemoveOsEnv

func RemoveOsEnv(key string) error

RemoveOsEnv remove a single environment variable. Play: https://go.dev/play/p/fqyq4b3xUFQ

Example
err1 := SetOsEnv("foo", "abc")
result1 := GetOsEnv("foo")

err2 := RemoveOsEnv("foo")
result2 := GetOsEnv("foo")

fmt.Println(err1)
fmt.Println(err2)
fmt.Println(result1)
fmt.Println(result2)
Output:

<nil>
<nil>
abc

func SetOsEnv

func SetOsEnv(key, value string) error

SetOsEnv sets the value of the environment variable named by the key. Play: https://go.dev/play/p/D88OYVCyjO-

Example
err := SetOsEnv("foo", "abc")
result := GetOsEnv("foo")

fmt.Println(err)
fmt.Println(result)
Output:

<nil>
abc

func StartProcess added in v2.3.3

func StartProcess(command string, args ...string) (int, error)

StartProcess start a new process with the specified name and arguments. Play: https://go.dev/play/p/5GVol6ryS_X

Example
pid, err := StartProcess("sleep", "2")
if err != nil {
	return
}

fmt.Println(pid)

func StopProcess added in v2.3.3

func StopProcess(pid int) error

StopProcess stop a process by pid. Play: https://go.dev/play/p/jJZhRYGGcmD

Example
pid, err := StartProcess("sleep", "10")
if err != nil {
	return
}
time.Sleep(1 * time.Second)

err = StopProcess(pid)

fmt.Println(err)
Output:

<nil>

Types

type Option added in v2.1.19

type Option func(*exec.Cmd)

func WithForeground added in v2.1.19

func WithForeground() Option

func WithWinHide added in v2.1.20

func WithWinHide() Option

type ProcessInfo added in v2.3.3

type ProcessInfo struct {
	PID                int
	CPU                string
	Memory             string
	State              string
	User               string
	Cmd                string
	Threads            []string
	IOStats            string
	StartTime          string
	ParentPID          int
	NetworkConnections string
}

ProcessInfo contains detailed information about a process.

func GetProcessInfo added in v2.3.3

func GetProcessInfo(pid int) (*ProcessInfo, error)

GetProcessInfo retrieves detailed process information by pid. Play: https://go.dev/play/p/NQDVywEYYx7

Example
pid, err := StartProcess("ls", "-a")
if err != nil {
	return
}

processInfo, err := GetProcessInfo(pid)
if err != nil {
	return
}

fmt.Println(processInfo)

Jump to

Keyboard shortcuts

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