From dbdd22c2acb7b2f115cd1de01331d12a4766ab49 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 21 Oct 2025 14:34:03 -0400 Subject: [PATCH] Basic idea for nohup started but needs more research. --- nohup/.gitignore | 2 ++ nohup/Makefile | 3 +++ nohup/go.mod | 7 +++++++ nohup/go.sum | 4 ++++ nohup/main.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+) create mode 100644 nohup/.gitignore create mode 100644 nohup/Makefile create mode 100644 nohup/go.mod create mode 100644 nohup/go.sum create mode 100644 nohup/main.go diff --git a/nohup/.gitignore b/nohup/.gitignore new file mode 100644 index 0000000..698ab92 --- /dev/null +++ b/nohup/.gitignore @@ -0,0 +1,2 @@ +nohup +nohup.exe diff --git a/nohup/Makefile b/nohup/Makefile new file mode 100644 index 0000000..e53d913 --- /dev/null +++ b/nohup/Makefile @@ -0,0 +1,3 @@ + +build: + go build . diff --git a/nohup/go.mod b/nohup/go.mod new file mode 100644 index 0000000..f983e70 --- /dev/null +++ b/nohup/go.mod @@ -0,0 +1,7 @@ +module lcthw.dev/go/go-coreutils/nohup + +go 1.25.3 + +require github.com/mattn/go-isatty v0.0.20 + +require golang.org/x/sys v0.6.0 // indirect diff --git a/nohup/go.sum b/nohup/go.sum new file mode 100644 index 0000000..46614ef --- /dev/null +++ b/nohup/go.sum @@ -0,0 +1,4 @@ +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/nohup/main.go b/nohup/main.go new file mode 100644 index 0000000..74ecbf0 --- /dev/null +++ b/nohup/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "github.com/mattn/go-isatty" + "flag" + "log" + "io" +) + +func main() { + flag.Parse() + prog := flag.Args()[0] + + // ignore SIGHUP + + if isatty.IsTerminal(os.Stdout.Fd()) { + fmt.Println("Is Terminal") + } else if isatty.IsCygwinTerminal(os.Stdout.Fd()) { + fmt.Println("Is cygwin/msys2 terminal") + } else { + cmd := exec.Command(prog) + if cmd.Err != nil { log.Fatal(cmd.Err) } + + in, err := cmd.StdinPipe() + if err != nil { log.Fatal(err) } + in.Close() + + stdout, err := cmd.StdoutPipe() + if err != nil { log.Fatal(err) } + + stderr, err := cmd.StderrPipe() + if err != nil { log.Fatal(err) } + + output := io.MultiReader(stdout, stderr) + + err = cmd.Start() + if err != nil { log.Fatal(err) } + + _, err = io.Copy(os.Stdout, output) + if err != nil { log.Fatal(err) } + } +}