Grunt exec running a node command in background - node.js

I'm trying to run grunt with a js script. The script start up a server that should be listening all the time in background. To it I'm using grunt and grunt-exec, and my exec.js is like that
start: {
cmd: function () {
var start = 'node server.js &';
console.info('Starting');
return start;
}
}
When I run grunt exec:start the process finish but the server is not running.
Same command without '&' works but it's not the wanted.
I've tried to move the command to a bash but the result is the same, the server is never started when the character '&' is added, neither in the .sh nor in the grunt-exec command.

Finally I solved it changing the command:
var start = "nohup node server.js";
I.t seems run in background properly.

Related

Nodejs child_process exec not running /bin/bash

I am trying to run a bash instance from node js but nothing seems to happen when I run the following code:
const { execSync } = require("child_process");
execSync("/bin/bash");
Then when I run ps to see if there is another bash process running there is no extra bash process running.

node exec command doesn't run under pm2

I have a very simple node script that waits for an event and then runs an ssh command:
function buildSite(){
exec('/usr/bin/ssh user#server \' cd SiteDir; ./build.sh \' ', function(error, stdout, stderr) {
console.log(stdout)
}); }
If the app is running with nodemon, this works. If I start the app with PM2, I get an event that tells me the command was received, but the site wasn't rebuilt as expected.
Am I missing something and need to accommodate for PM2?

Node.js ChildProcess#kill() does not kill Node.js processes created by npm.cmd on Windows

I am trying to kill child processes created by command npm.cmd (spawned using Node.js API) on Windows.
It executes something like npm.cmd run build. Using process.kill() seems to be able to end the npm.cmd command successfully, but not the Node.js processes created by the npm command.
let cp = spawn('npm.cmd', ['run', 'build']);
cp.on('exit', code => {
// It will log after 5 seconds.
// But Node.js process created by `npm run build` will not end.
console.log(`Exited with code ${code}.`);
});
setTimeout(() => cp.kill(), 5000);
I tried to replace process.kill() to something like spawn('taskkill', ['/f', '/t', '/pid', process.pid]) and it worked well. How can I achieve the same thing using Node.js API?

How can I run grunt as a daemon?

I am running a packaged nodejs webserver that allows for reading of epub files (Readium-JS), and it is started with the grunt command.
However, if I run this on my VPS the server dies as soon as my terminal connection ends.
How can I run this task as a daemon?
I have looked at options like grunt-forever and grunt-daemon but the way the Gruntfile is written using load-grunt-config is messing with my mind and I can't piece together how to isolate the server code.
Here's the solution I found:
As was suggested above, using pm2
However, when I ran
pm2 start grunt
I got an error saying that the grunt module did not exist, which was weird.
So I ended up writing a script which worked:
-- start.js --
var pm2 = require('pm2');
pm2.connect(function() {
pm2.start({
script : '/usr/local/bin/grunt', // Script to be run
args: '--force',
}, function(err, apps) {
pm2.disconnect();
});
});
After running node start.js from the command line, everything sailed smoothly.

node.js child process change a directory and run the process

I try to run external application in node.js with child process like the following
var cp = require("child_process");
cp.exec("cd "+path+" && ./run.sh",function(error,stdout,stderr){
})
However when I try to run it stuck, without entering the callback
run.sh starts a server, when I execute it with cp.exec I expect it run asynchronously, such that my application doesn't wait until server termination. In callback I want to work with server.
Please help me to solve this.
cp.exec get the working directory in parameter options
http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_exec_command_options_callback
Use
var cp = require("child_process");
cp.exec("./run.sh", {cwd: path}, function(error,stdout,stderr){
});
for running script in the "path" directory.
The quotes are interpreted by the shell, you cannot see them if you just look at ps output.

Resources