Hello, I want to run perl script as background process and redirect its output to a file.
Here is an example of print
script
#!/usr/bin/perl
$i = 10; while($i--) {print time()."\n"; sleep 5;}
Here is how I run it:
package main
import (
"fmt"
"os"
)
func main() {
file, err := os.OpenFile("/tmp/out.log", os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
fmt.Println("failed to open log file")
return
}
attr := os.ProcAttr{
Env: os.Environ(),
Files: []*os.File{
nil,
file,
file,
},
}
process, err := os.StartProcess("/tmp/print", []string{"/tmp/print"}, &attr)
if err != nil {
fmt.Println("failed to start process: %v", err)
return
}
err = process.Release()
if err != nil {
fmt.Println("failed to release: %v", err)
}
}
This works, but the out.log
file is not filled until the script has finished. If I use os.Stdout
instead of a file, then the output is printed immediately line by line.
Since the script is supposed to run for a long time, I would like to see the actual output in the log file. Is there a way to do this?