3

I am trying to integrate some node.js code with my Rails application. Basically its a js file with some code that process will keep running in background.

I have followed the following steps:

  • Added code in root of rails app in some test_node.js file.
  • Now what I do is pass a value to my system using exec function of ruby, e.g exec "node test_node.js".
  • This works perfectly fine but it chop my server from processing any requests.
  • To pass it to background i tried using nohup e.g: exec "nohup node test_node.js".
  • When I run this code my server crashes.

I am Rails developer and never worked on node app so have no idea if I taking it right way or not.

peeyush singla
  • 557
  • 2
  • 8
  • 20
  • 2
    What server? Did you used `fork and exec`? Can you rewrite test_node.js in ruby? – radubogdan May 01 '15 at 09:29
  • I am using Unicorn for my application, its not possible to rewrite test_node.js in Ruby. I tried using `fork and exec` it worked but as I checked in logs I feel it stopped server and forked it again. Will it work on production as well? – peeyush singla May 01 '15 at 09:36

1 Answers1

4

exec replaces the currently running process with a new one. Thus, to run something in the background you should fork and exec in the child.

fork { exec('node', 'test_node.js', ...) }

nohup is not needed.

See also Ruby, Difference between exec, system and %x() or Backticks

user49740
  • 383
  • 1
  • 11