dovzrfr0506 2017-10-19 12:56
浏览 24
已采纳

最终流程和子流程

I'm trying to properly terminate a command

c := exec.Command("omxplayer", "video.mp4")
c.Start()

// send kill signal to terminate command at later stage
time.Sleep(4*time.Second)
c.Process.Kill()
c.Wait() // should wait for program to fully exit

// start again with new video
c := exec.Command("omxplayer", "video2.mp4")
c.Start()

I'm trying to kill the current omxplayer process on my Raspberry-Pi so that I can start it again with a new video.

Once I send the Kill signal, I call c.Wait() to wait for the current command to end before starting a new command.

The Problem is that the first command is not stopping, but the next command is starting anyway. So I end up with multiple videos being played at the same time.

  • 写回答

1条回答 默认 最新

  • doubu7134 2017-10-24 15:55
    关注

    omxplayer is forking a new process. For omxplayer in particular, I can send the "q" string into its StdinPipe. That's like pressing the q key on the keyboard, which quits the program, ending both the parent and child processes:

    c := exec.Command("omxplayer", "video.mp4")
    c.Start()
    p := c.StdinPipe()
    _, err := p.Write([]byte("q")) // send 'q' to the Stdin of the process to quit omxplayer
    
    c.Wait() // should wait for program to fully exit
    
    // start again with new video
    c := exec.Command("omxplayer", "video2.mp4")
    c.Start()
    

    Another more general option is to create a process group with both the parent and child processes and kill that:

    c := exec.Command("omxplayer", "video.mp4")
    // make the process group id the same as the pid
    c.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
    c.Start()
    
    // sending a negative number as the PID sends the kill signal to the group
    syscall.Kill(-c.Process.Pid, syscall.SIGKILL)
    
    c.Wait()
    

    Based on this Medium post

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?