Skip to content

Instantly share code, notes, and snippets.

@maurorappa
Created February 20, 2018 20:55
Show Gist options
  • Save maurorappa/20e089ad32548022eb7072b9b94372f3 to your computer and use it in GitHub Desktop.
Save maurorappa/20e089ad32548022eb7072b9b94372f3 to your computer and use it in GitHub Desktop.
utility to check an output file and stop the program when the desired size is reached
// This utility allows you to run a command which produce a file and interrup it when a certain size is reached
package main
import(
"flag"
"fmt"
"log"
"os"
"os/exec"
"time"
)
func main(){
interval := flag.Int("i",200,"check interval")
delay := flag.Int("d",5,"initial delay")
size := flag.Int("s",5000,"desired file size")
filename := flag.String("f","","file to monitor")
tool := flag.String("c","","command to run")
flag.Parse()
ticker := time.NewTicker(time.Duration(*interval) * time.Millisecond)
//this goroutine checks the size of the file
go func() {
time.Sleep(time.Duration(*delay) * time.Duration(*interval) * time.Millisecond)
fmt.Println("Watcher started")
for {
select {
case <- ticker.C :
stat, err := os.Stat(*filename)
if err != nil {
log.Fatal(err)
}
fmt.Printf("size: %d\n", stat.Size() )
if stat.Size() > int64(*size) {
fmt.Println("Desired size reached!")
os.Exit(0)
}
}
}
}()
//this goroutine runs the command which will produce the file we want
go func() {
fmt.Println("Running %v", *tool)
cmd := exec.Command("sh","-c",*tool)
return_value := cmd.Run()
log.Printf("Command returned: %v", return_value)
}()
for{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment