2017-10-19 2 views
0

내가 제대로 나는 새로운 비디오 다시 그것을 시작할 수 있도록 내 나무 딸기 - 파이의 현재 omxplayer 프로세스를 종료하기 위해 노력하고있어 명령을프로세스 종료와 아이

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() 

을 종료하기 위해 노력하고있어 처리 .

Kill 신호를 보내면 c.Wait()으로 전화하여 새 명령을 시작하기 전에 현재 명령이 끝날 때까지 기다려야합니다.

문제은 첫 번째 명령이 중지되지 않고 다음 명령이 시작되는 것입니다. 그래서 나는 동시에 여러 개의 비디오를 재생하게됩니다.

+2

'을 바탕으로

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() 

. 이 경우, 종료 할 메커니즘을 제공하지 않으면 신호를 보내기 위해 pid를 검색해야 할 수 있습니다. – JimB

+0

'pstree -p'를 사용하면'omxplayer'에 의해 fork 된 프로세스를 볼 수 있습니다 (있는 경우). – putu

+0

부모 프로세스도 정리를 원할 수 있지만 SIGKILL을 보내서는 허용하지 않습니다. SIGTERM 또는 SIGINT로 어떤 일이 일어나는지보십시오. – JimB

답변

1

omxplayer은 새로운 프로세스를 만듭니다. 특히 omxplayer의 경우 "q"문자열을 StdinPipe으로 보낼 수 있습니다.

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() 

또 다른 일반적인 옵션은 모두 부모와 자식 프로세스와 프로세스 그룹을 생성하고 있음을 죽일 것입니다 : 그것은 모두 부모와 자식 프로세스를 종료, 프로그램을 종료 키보드의 q 키를 눌러처럼 : 아마 새로운 프로세스를 분기됩니다 omxplayer` this Medium post