summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHorus32015-02-27 19:28:22 +0100
committerHorus32015-02-27 19:28:22 +0100
commitfe72ef7e03c0df883f8b120c77597f508509004a (patch)
tree408b4f552c42e503324c6da2a4983dcf473a0533
downloadmonotond-fe72ef7e03c0df883f8b120c77597f508509004a.tar.gz
Small daemon to repetitive execute a program.
-rw-r--r--.gitignore4
-rw-r--r--monoton.go56
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 {}
+ }
+}