-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (87 loc) · 2.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"log"
"os"
"os/exec"
"strings"
"github.com/robfig/cron/v3"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "scheduler",
Usage: "A simple command scheduler",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "command",
Usage: "Command to run",
Required: true,
},
&cli.StringFlag{
Name: "schedule",
Usage: `Cron schedule.
Example (every 5 seconds): 0/5 * * * * *
See more https://crontab.cronhub.io/ or https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm`,
Required: true,
},
&cli.StringFlag{
Name: "next-command",
Usage: "Next command to run, while scheduler is running",
Required: false,
},
&cli.BoolFlag{
Name: "verbose",
Usage: "Verbose output",
Required: false,
},
},
Action: func(c *cli.Context) error {
command := c.String("command")
schedule := c.String("schedule")
nextCommand := c.String("next-command")
verbose := c.Bool("verbose")
cron := cron.New(cron.WithSeconds())
_, err := cron.AddFunc(schedule, func() {
runCommand(command, verbose)
})
if err != nil {
return err
}
cron.Start()
if nextCommand != "" {
runCommand(nextCommand, verbose)
}
select {} // Block forever
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func runCommand(command string, verbose bool) {
if verbose {
log.Printf("Running command: %s\n", command)
}
commandElements := strings.Split(command, " ")
if len(commandElements) == 0 {
log.Printf("Missing command")
return
}
var args []string
for _, arg := range commandElements[1:] {
arg = strings.Trim(arg, "\"")
args = append(args, strings.Trim(arg, "'"))
}
cmd := exec.Command(commandElements[0], args...)
output, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
log.Printf("Command exited with code %d and error: %s\n", exitErr.ExitCode(), exitErr.Stderr)
return
}
log.Printf("Failed to execute command: %v, error: %v\n", command, err)
return
}
log.Printf("%s\n", output)
}