Start detached command with redirect to file

前端 未结 2 1134
心在旅途
心在旅途 2021-01-15 05:59

I\'m trying to start a command in a detached process so that it can continue after go program exits. I need to redirect the output of the command to a file.

What I n

2条回答
  •  长情又很酷
    2021-01-15 06:15

    You may start a shell which executes your command / app, and you may redirect its output to a file. The shell will continue to run and execute your script / app even if your Go app exits.

    Example:

    cmd := exec.Command("sh", "-c", "/tmp/test.sh > /tmp/out")
    if err := cmd.Start(); err != nil {
        panic(err)
    }
    fmt.Println("Process ID:", cmd.Process.Pid)
    

    Test it with this simple Go app (replace /tmp/test.sh with the name of the executable binary you compile this into):

    package main
    import ("fmt"; "time")
    func main() {
        for i := 0; i < 10; i++ {
            fmt.Printf("%d.: %v\n", i, time.Now())
            time.Sleep(time.Second)
        }
    }
    

    This app simply prints a line to the standard output once every second. You can see how the output file is being written e.g. with tail -f /tmp/out.

    Note that you may use other shells to execute your scripts to your liking (and to what the test.sh script dictates).

    For example to use bash:

    cmd := exec.Command("/bin/bash", "-c", "/tmp/test.sh > /tmp/out")
    // rest is unchanged
    

    Note that the command to be executed by the shell is passed as a single string argument, and it is not broken down into multiple as you would do it if you were to execute it directly in the command prompt.

提交回复
热议问题