cron and go-daemon? help plz

polaris · · 382 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>``` package main</p> <pre><code>import ( &#34;fmt&#34; &#34;; &#34;; &#34;log&#34; &#34;os&#34; ) const ( // name of the service name = &#34;cronTool&#34; description = &#34;Cron service task&#34; ) 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(&#34;* * * * * *&#34;, func() { makeFile() }) c.Start() } func stopCron(c *cron.Cron) { c.Stop() } var times int func makeFile() { times++ f, err := os.Create(fmt.Sprintf(&#34;%d.txt&#34;, 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 := &#34;Usage: cronStock install | remove | start | stop | status&#34; // if received any kind of command, do it if len(os.Args) &gt; 1 { command := os.Args[1] switch command { case &#34;install&#34;: startCron(&amp;service.c) return service.d.Install() case &#34;remove&#34;: return service.d.Remove() case &#34;start&#34;: startCron(&amp;service.c) return service.d.Start() case &#34;stop&#34;: stopCron(&amp;service.c) return service.d.Stop() case &#34;status&#34;: return service.d.Status() default: return usage, nil } } // // c.AddFunc(&#34;@weekly&#34;, func() {}) // my actual usage will be as follows return usage, nil } func init() { stdlog = log.New(os.Stdout, &#34;&#34;, log.Ldate|log.Ltime) errlog = log.New(os.Stderr, &#34;&#34;, log.Ldate|log.Ltime) } func main() { c := cron.New() startCron(c) srv, err := daemon.New(name, description) if err != nil { errlog.Println(&#34;Error: &#34;, err) os.Exit(1) } service := &amp;Service{srv, *c} status, err := service.Manage() if err != nil { errlog.Println(status, &#34;\nError: &#34;, err) os.Exit(1) } fmt.Println(status) } </code></pre> <p>```</p> <p>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 <code>sudo ./cronTool install</code> and <code>sudo ./cronTool start</code> seem to do nothing. </p> <p><strong>Edit:</strong> it works fine with <code>cron.Run()</code> rather than <code>cron.Start()</code> 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</p> <p><strong>This is a dup from Github, figured i&#39;d ask here since no one has responded in repo</strong></p> <hr/>**评论:**<br/><br/>ChristophBerger: <pre><p>I had a peek into the source code and found that both Start() and Run() call the function <a href="" rel="nofollow">run()</a> <em>that goes into an infinte loop.</em> So it seems that your binary needs to stay up and running as long as any scheduling shall take place.</p></pre>maximus12793: <pre><p>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 :( </p></pre>ChristophBerger: <pre><p>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?</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

382 次点击  
加入收藏 微博
0 回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传