2014-04-09 09:56



I execute process with Go and write output to file (log file)

    cmd := exec.Command(path)
    cmd.Dir = dir
    t := time.Now()
    t1 := t.Format("20060102-150405")

    fs, err := os.Create(dir + "/var/log/" + t1 + ".std")
    if err == nil {
        cmd.Stdout = fs

I wish to rotate logs and change log file daily http://golang.org/pkg/os/exec/

    // Stdout and Stderr specify the process's standard output and error.
    // If either is nil, Run connects the corresponding file descriptor
    // to the null device (os.DevNull).
    // If Stdout and Stderr are the same writer, at most one
    // goroutine at a time will call Write.
    Stdout io.Writer
    Stderr io.Writer

Is it safe to change cmd.Stdout variable daily from arbitary goroutine or I have to implement goroutine that will copy from Stdout to another file and switch files?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dstobkpm908182 dstobkpm908182 7年前

    It is safe to change those variables directly. However, if you change them once the command has actually been run then they will have no effect on the actual running child process. To rotate the output of the running process "live" you will have to implement that in the process itself, or pipe everything through the parent and use a goroutine as you suggest.

    点赞 评论 复制链接分享