Skip to content

Instantly share code, notes, and snippets.

@mrunalp
Created June 28, 2017 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrunalp/b56180e67093d66de7a191932bb4b7fe to your computer and use it in GitHub Desktop.
Save mrunalp/b56180e67093d66de7a191932bb4b7fe to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"os"
"syscall"
"time"
)
func main() {
console, err := os.OpenFile("/dev/console", os.O_RDWR, 0)
if err != nil {
log.Fatal(err)
}
fmt.Fprintln(console, "write before dup /dev/null to stdio")
makeNullStdio()
fmt.Fprintln(console, "write after dup /dev/null to stdio")
console.Close()
logf, _ := os.Create("/tmp/logfile")
for {
console, err = os.OpenFile("/dev/console", os.O_RDWR, 0)
if err != nil {
fmt.Fprint(logf, err)
os.Exit(1)
}
fmt.Fprintln(console, "open, write to console, and close")
console.Close()
time.Sleep(time.Second)
}
}
func makeNullStdio() {
nullFd, _ := os.OpenFile("/dev/null", os.O_RDWR, 0)
defer nullFd.Close()
fd := int(nullFd.Fd())
syscall.Dup2(fd, 0)
syscall.Dup2(fd, 1)
syscall.Dup2(fd, 2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment