[Question] exec inside a goroutine

agolangf · · 468 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I wrote an app that terminates the browser after 10 minutes. I want to display a desktop notification using <code>notify-send</code> and <code>os/exec</code>. However the notification is never displayed. Here&#39;s the code</p> <pre><code>package main import ( &#34;os/exec&#34; &#34;time&#34; &#34;github.com/shirou/gopsutil/process&#34; ) func main() { ticker := time.NewTicker(5 * time.Second) for range ticker.C { terminateBrowser() } } func terminateBrowser() { pids, _ := process.Pids() for _, pid := range pids { p, _ := process.NewProcess(pid) name, _ := p.Name() if name == &#34;firefox&#34; || name == &#34;chrome&#34; { go func(p *process.Process, name string) { time.Sleep(10 * time.Minute) p.Terminate() exec.Command(&#34;notify-send&#34;, name, name+&#34; quit unexpectedly&#34;).Run() }(p, name) } } } </code></pre> <p>edit: i figured it out. i changed <code>exec.Command(&#34;notify-send&#34;, name, name+&#34; quit unexpectedly&#34;).Run()</code> to <code>exec.Command(&#34;notify-send&#34;, name, name+&#34; quit unexpectedly&#34;).Start()</code> so that it wont wait for it to complete.</p> <hr/>**评论:**<br/><br/>driusan: <pre><ol> <li>Your main function is likely terminating before the go routine gets scheduled. If you need to use goroutines, consider using a sync.WaitGroup in terminateBrowser.</li> <li>You&#39;re not running the command. See the examples in the go docs of os/exec for how to use exec.Command</li> </ol></pre>newbgopher: <pre><ol> <li>the <code>main</code> function is not terminating since it has a <code>ticker</code> that ticks every five seconds. if you run the code, you&#39;ll see that it doesn&#39;t exit unless you press <code>CTRL+C</code>. I&#39;ll try to add a <code>sync.WaitGroup</code>.</li> </ol> <p>2 . didn&#39;t you see the <code>Run()</code> call at the end?</p></pre>driusan: <pre><p>Oh, sorry. reddit cut off the Run call in the div on mobile unless you manually scroll over. It literally cut it off at the <code>)</code> after the Command argument..</p></pre>newbgopher: <pre><p>no worries fam</p></pre>printf_hello_world: <pre><p>You probably just need to wait for the termination goroutines to finish before exiting the main function.</p> <p>I&#39;d probably do this with a sync.WaitGroup</p> <p>Could pass a *sync.Waitgroup into your function, call Add(1) on it, and call Done() on it in the goroutine as it returns.</p> <p>Then just call Wait() after your loop in main.</p></pre>0xjnml: <pre><blockquote> <p>However the notification is never displayed.</p> </blockquote> <p>Check and handle errors.</p></pre>newbgopher: <pre><p>i log those errors in my code, i just remove it here for brevity. there&#39;s no error logged when i ran it though.</p></pre>

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

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