Home > database >  Ruby spawn a process that outlives the lifetime of the program
Ruby spawn a process that outlives the lifetime of the program

Time:08-28

I have a program, where I am starting a program called mpv. I want to use the shell to start mpv with a video, and then exit my ruby program. My problem is that currently, the mpv instance is killed the the ruby program ends. How can I fix this?

# do some stuff
system 'mpv \"#{video_url}" & disown'
# end of program

I have also tried using

pid = Process.spawn "mpv \"#{video_url}\""
Process.detach pid

The program is that both of these will make the mpv process be killed immediately. If I do not exit, or if I sleep at the end of the program, the video plays until the program ends. How can I make spawning of mpv last longer than my ruby program?

CodePudding user response:

You can just use exec(). It will terminate your ruby program and execute the given command

exec('mpv \"#{video_url}\"')

or you can use spawn too

spawn exec "mpv \"#{video_url}\" &"
  •  Tags:  
  • ruby
  • Related