How to manage (run / kill) the background process (server application) in ruby

I am trying to configure the server for integration tests (actually actually) through ruby ​​and cannot figure out how to control the process.

So, I am trying to do this:

  • run the rake task for my gem that fulfills integration specifications
  • the task is to start the server first (I use webrick) and then run the specifications
  • after fulfilling the specifications, he should kill webrick so that I do not stay with some unused background process.

webrick is not a requirement, but it is included in the ruby ​​standard library, so the ability to use it will be great.

hope someone can help!

ps. I work on Linux, so this work for Windows is not my top priority (right now).

+7
ruby process webrick
Jan 02 '09 at 22:18
source share
3 answers

The standard way is to use the system functions fork (to duplicate the current process), exec (to replace the current process with an executable file) and kill (to send a signal to the process to complete it).

For example:

pid = fork do # this code is run in the child process # you can do anything here, like changing current directory or reopening STDOUT exec "/path/to/executable" end # this code is run in the parent process # do your stuffs # kill it (other signals than TERM may be used, depending on the program you want # to kill. The signal KILL will always work but the process won't be allowed # to cleanup anything) Process.kill "TERM", pid # you have to wait for its termination, otherwise it will become a zombie process # (or you can use Process.detach) Process.wait pid 

This should work on any Unix-like system. Windows creates a process differently.

+12
Jan 02 '09 at 23:07
source share

I just needed to do something similar, and this is what I came up with. @Michael Witrant's answer got me started, but I changed some things like using Process.spawn instead of fork ( newer and better ).

 # start spawns a process and returns the pid of the process def start(exe) puts "Starting #{exe}" pid = spawn(exe) # need to detach to avoid daemon processes: http://www.ruby-doc.org/core-2.1.3/Process.html#method-c-detach Process.detach(pid) return pid end # This will kill off all the programs we started def killall(pids) pids.each do |pid| puts "Killing #{pid}" # kill it (other signals than TERM may be used, depending on the program you want # to kill. The signal KILL will always work but the process won't be allowed # to cleanup anything) begin Process.kill "TERM", pid # you have to wait for its termination, otherwise it will become a zombie process # (or you can use Process.detach) Process.wait pid rescue => ex puts "ERROR: Couldn't kill #{pid}. #{ex.class}=#{ex.message}" end end end # Now we can start processes and keep the pids for killing them later pids = [] pids << start('./someprogram') # Do whatever you want here, run your tests, etc. # When you're done, be sure to kill of the processes you spawned killall(pids) 

What about everything she wrote, try it and let me know how it works.

+2
Oct 08 '14 at 17:34
source share

I tried fork, but it has problems when ActiveRecord is involved in both processes. I would suggest the Spawn plugin ( http://github.com/tra/spawn ). It only makes a plug, but takes care of ActiveRecord.

0
Jan 03 '09 at 9:32
source share



All Articles