Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start and stop a shell daemon from Ruby

Tags:

ruby

How would I start a shell daemon and then stop it from within the same Ruby script?

To be more specific, I'd like my Ruby script to:

  • start a server daemon with a shell command (rails server | shotgun | etc... ).
  • do a couple things while the server is running.
  • make the server quit.

All embracing solutions that are supported in ruby 1.8/1.9 and unix/windows are preferred. (I read fork for instance does not work on Windows).

like image 808
Steph Thirion Avatar asked Feb 07 '11 00:02

Steph Thirion


People also ask

How do I run a shell script in Ruby?

Create configurations for script files and select Shell Script. Under Execute, select the Script file option. Specify the path to the script file and options that you want to pass to the script when it is launched. You can also change the interpreter for running the script and additional options for the interpreter.

What is daemon shell?

A daemon (also known as background processes) is a Linux or UNIX program that runs in the background. Almost all daemons have names that end with the letter "d". For example, httpd the daemon that handles the Apache server, or, sshd which handles SSH remote access connections.


1 Answers

I can highly recommend the Ruby Daemons gem. I think it has all the functionality you need.

Check the section 3 of its README, and pay particular attention to Daemons.call method that allows to daemonize arbitrary code (in which you could just Kernel.system an app).

like image 69
Peter Avatar answered Oct 28 '22 16:10

Peter