diff options
| author | Horus3 | 2015-02-27 19:28:22 +0100 |
|---|---|---|
| committer | Horus3 | 2015-02-27 19:28:22 +0100 |
| commit | fe72ef7e03c0df883f8b120c77597f508509004a (patch) | |
| tree | 408b4f552c42e503324c6da2a4983dcf473a0533 | |
| download | monotond-fe72ef7e03c0df883f8b120c77597f508509004a.tar.gz | |
Small daemon to repetitive execute a program.
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | monoton.go | 56 |
2 files changed, 60 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f9b4c28 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.swp +*.db +monoton +_/ diff --git a/monoton.go b/monoton.go new file mode 100644 index 0000000..b19e23f --- /dev/null +++ b/monoton.go @@ -0,0 +1,56 @@ +package main + +import ( + "flag" + "fmt" + "github.com/robfig/cron" + "log" + "os" + "os/exec" +) + +func main() { + time_f := flag.String("time", "1m", "Set the time interval.") + cmd_f := flag.String("cmd", "", "Execute the command.") + vars_f := flag.String("vars", "", "Command line options for the programm.") + + flag.Parse() + var ( + time, cmd, vars string + ) + + time = fmt.Sprintf("@every %s", *time_f) + cmd = fmt.Sprintf("%s", *cmd_f) + vars = fmt.Sprintf("%s", *vars_f) + + if _, err := os.Stat(cmd); os.IsNotExist(err) { + fmt.Println(err) + os.Exit(1) + } + + job := cron.New() + job.AddFunc(time, func() { + + if vars == "" { + c := exec.Command(cmd) + o, err := c.Output() + if err != nil { + log.Println(err) + } + fmt.Printf("%s", o) + } else { + c := exec.Command("bash", "-c", cmd+" "+vars) + o, err := c.Output() + if err != nil { + log.Println(err) + } + fmt.Printf("%s", o) + } + + }) + job.Start() + + for { + select {} + } +} |
