Forum: Ruby question about the system method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4a691970d23098ca00ec5780774ace4b?d=identicon&s=25 Jarod Zhu (Guest)
on 2008-12-01 18:35
(Received via mailing list)
Hi, all

     I want to use system method to run a background process, like

     system("ruby deamon.rb") # the deamon.rb runs a dead loop

     When my main process ends, I want to end the background process
too.

     How can I do it?

     If I can get the pid of background process, I can use
Process.kill. But the system method dosen't return the pid.

     Does anyone here know the solution?

     Thank you

Jarod Zhu
4dea430d31b993abaf41cd9b54f8128d?d=identicon&s=25 Avdi Grimm (avdi)
on 2008-12-01 18:38
(Received via mailing list)
On Mon, Dec 1, 2008 at 12:29 PM, Jarod Zhu <zhujie2003info@gmail.com>
wrote:
> Hi, all
>
>     I want to use system method to run a background process, like
>
>     system("ruby deamon.rb") # the deamon.rb runs a dead loop
>
>     When my main process ends, I want to end the background process
> too.
>
>     How can I do it?

The basic answer is to use IO.popen() - see
http://www.ruby-doc.org/core/classes/IO.html#M002267

For more advances applications you may want to take a look at Daemons:
http://daemons.rubyforge.org/

--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com
4a691970d23098ca00ec5780774ace4b?d=identicon&s=25 Jarod Zhu (Guest)
on 2008-12-02 02:45
(Received via mailing list)
IO.popen() works well.
Thanks a lot!!

Jarod Zhu
This topic is locked and can not be replied to.