Skip to content

Instantly share code, notes, and snippets.

@ericchiang
Last active January 26, 2018 18:05
Show Gist options
  • Save ericchiang/4f8a99e7ad072f932a40d45ad499c347 to your computer and use it in GitHub Desktop.
Save ericchiang/4f8a99e7ad072f932a40d45ad499c347 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"os/exec"
isatty "github.com/mattn/go-isatty"
)
func reExec(args ...string) error {
cmd := exec.Command("/proc/self/exe", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
return fmt.Errorf("run command: %v", err)
}
return nil
}
// This program prints "Is Terminal"
func main() {
if len(os.Args) > 1 && os.Args[1] == "reexec" {
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Fprintln(os.Stderr, "Is Terminal")
} else {
fmt.Fprintln(os.Stderr, "Is Not Terminal")
}
return
}
if err := reExec("reexec"); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment