nohup should work but need linux to really test it.

master
Zed A. Shaw 6 days ago
parent dbdd22c2ac
commit a7e63839b1
  1. 2
      nohup/.gitignore
  2. 42
      nohup/main.go

2
nohup/.gitignore vendored

@ -1,2 +1,4 @@
nohup
nohup.exe
*.out
date.exe

@ -1,27 +1,18 @@
package main
import (
"fmt"
"os"
"os/exec"
"github.com/mattn/go-isatty"
"flag"
"log"
"io"
"os/signal"
"syscall"
)
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)
func Exec(prog string, args []string, target_out io.Writer) {
cmd := exec.Command(prog, args...)
if cmd.Err != nil { log.Fatal(cmd.Err) }
in, err := cmd.StdinPipe()
@ -39,7 +30,30 @@ func main() {
err = cmd.Start()
if err != nil { log.Fatal(err) }
_, err = io.Copy(os.Stdout, output)
_, err = io.Copy(target_out, output)
if err != nil { log.Fatal(err) }
}
func OpenOutput() io.Writer {
fd := os.Stdout.Fd()
is_atty := isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
if is_atty {
// create nohup
out_file, err := os.OpenFile("nohup.out", os.O_RDWR | os.O_CREATE | os.O_TRUNC, 0644)
if err != nil { log.Fatal(err) }
return out_file
} else {
return os.Stdout
}
}
func main() {
flag.Parse()
args := flag.Args()
output := OpenOutput()
Exec(args[0], args[1:], output)
signal.Ignore(syscall.SIGHUP)
}

Loading…
Cancel
Save