``` package main
import (
"fmt"
"github.com/robfig/cron"
"github.com/takama/daemon"
"log"
"os"
)
const (
// name of the service
name = "cronTool"
description = "Cron service task"
)
var stdlog, errlog *log.Logger
// Service is the daemon service struct
type Service struct {
d daemon.Daemon
c cron.Cron
}
func startCron(c *cron.Cron) {
// Run 1x everymin
c.AddFunc("* * * * * *", func() { makeFile() })
c.Start()
}
func stopCron(c *cron.Cron) {
c.Stop()
}
var times int
func makeFile() {
times++
f, err := os.Create(fmt.Sprintf("%d.txt", times))
if err != nil {
log.Fatal(err)
}
defer f.Close()
}
// Manage by daemon commands or run the daemon
func (service *Service) Manage() (string, error) {
usage := "Usage: cronStock install | remove | start | stop | status"
// if received any kind of command, do it
if len(os.Args) > 1 {
command := os.Args[1]
switch command {
case "install":
startCron(&service.c)
return service.d.Install()
case "remove":
return service.d.Remove()
case "start":
startCron(&service.c)
return service.d.Start()
case "stop":
stopCron(&service.c)
return service.d.Stop()
case "status":
return service.d.Status()
default:
return usage, nil
}
}
// // c.AddFunc("@weekly", func() {}) // my actual usage will be as follows
return usage, nil
}
func init() {
stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime)
errlog = log.New(os.Stderr, "", log.Ldate|log.Ltime)
}
func main() {
c := cron.New()
startCron(c)
srv, err := daemon.New(name, description)
if err != nil {
errlog.Println("Error: ", err)
os.Exit(1)
}
service := &Service{srv, *c}
status, err := service.Manage()
if err != nil {
errlog.Println(status, "\nError: ", err)
os.Exit(1)
}
fmt.Println(status)
}
```
I am trying to simply run a daemon that starts a cronjob. I cannot see any examples of anything outside of reading from a channel so its unclear why this cron job is not being ran. Any ideas?
both sudo ./cronTool install
and sudo ./cronTool start
seem to do nothing.
Edit: it works fine with cron.Run()
rather than cron.Start()
but Run() blocks the daemon so I cannot detach reliably. Is there some issue with goroutines inside a daemon? I have tried keeping an event loop, passing cron into main or into the service struct. Everything seems to block or not execute
This is a dup from Github, figured i'd ask here since no one has responded in repo
评论:
ChristophBerger:
maximus12793:I had a peek into the source code and found that both Start() and Run() call the function run() that goes into an infinte loop. So it seems that your binary needs to stay up and running as long as any scheduling shall take place.
ChristophBerger:Yea, so that was why I was thinking to make it a daemon process. Do they not run until activated or something? I was thinking if I just threw an infinite loop after all my argument checks it would pick up the cron job and wait until the time passed, but that didnt seem to work :(
The daemon package clearly lacks a useful description, but the code example seems to indicate that the Manage() method is intended to do the daemon work (in an infinite loop) if the binary is invoked with no args. In your code, Manage() simply exits if there are no args, maybe this is the problem?
