Small problem executing a ruby script with execSync. #5

Closed
trusktr opened this Issue Feb 27, 2013 · 3 comments

Projects

None yet

2 participants

@trusktr
trusktr commented Feb 27, 2013

I have a ruby server that I execute from command line like this:

ruby script/rails server webrick -e production

When I try executing that command like in the following example, the ruby server quits:

var execSync = require('execSync');
var result = execSync.stdout('ruby script/rails server webrick -e production');
console.log(result);

However, if I use Nodejs's built-in child_process.exec method, then the ruby server runs just fine without stopping, like this:

var util = require('util');
var exec = require('child_process').exec;

exec("ruby script/rails server webrick -e production", function(error, stdout, stderr) {
        console.log(stdout);
        console.log('done.');
});

Any idea why it doesn't work with execSync but works fine with child_process.exec?

@trusktr
trusktr commented Feb 27, 2013

You might be wondering why "Why on earth are you using this to run a ruby server?".

The answer is because I figured using the forever module would be an easy way to keep the server alive if it crashes for some reason.

Turns out that using child_process.exec() works fine too, as the script won't exit until the command is done executing and the callback function has executed.

It'd still be nice to know why the ruby server exits under execSync.

By the way, the stdout from the ruby server shows no errors, just an "exiting" message as if ctrl+c was pressed. It must be detecting it' environment to be different in execSync than with child_process.exec.

@mgutz
Owner
mgutz commented Jul 12, 2013

Please try latest. Closing this ticket in a couple of days if no response.

@mgutz mgutz closed this Jul 14, 2013
@trusktr
trusktr commented Jul 14, 2013

Just tested, and now the server runs nicely. If the ruby server process exits, the call to execSync.exec() finishes as expected.

However, I noticed a problem. If the sh process that you are using to call the user's command exits, the user's command continues to run, while the call to execSync.exec() finishes.

e.g. if I do:

 var execSync = require('execSync');

 var result = execSync.exec('ruby script/rails server webrick -e production');

 console.log(result.code);
 console.log(result.stdout);

this creates a process by doing similar to

sh -c (ruby script/rails server webrick -e production) > /tmp/113614-1171-gxo0iy.exec 2>&1

and the my ruby server becomes a sub-process of this. If, for some reason, that call to sh crashes (probably unlikely), the server continues to run. As a weird side-effect, the ruby server can no longer be killed using the kill command, even as root, but using htop works for killing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment