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.

2
  • 2
    What server? Did you used fork and exec? Can you rewrite test_node.js in ruby? Commented May 1, 2015 at 9: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? Commented May 1, 2015 at 9:36

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

awesome, is there some way to check the logs of these process?
You can redirect stdout/stderr to a file right before the exec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.