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.
Related
I want to run a bat file with child_process module in NodeJS but I want the application to stop and the bat file to continue processing without waiting for the result of the application.
Code I used:
const { exec } = require("child_process");
exec("start cmd /c my.bat");
I'm working on a CLI tool to add an extra layer of automation and utilities to our workflow and I'm wrapping the webpack development command with an alternative in my CLI (here's a demonstration):-
function runDev(){
this.doSomePreAutomationAndPreperations();
this.runWebpackDevCommand();
}
I'm using NodeJs child_proecess.exec and I'm trying to figure out a way to execute the webpack dev command and attach it to the terminal (like -it in docker if you're familiar with it) or transferring the control to the child process(so output will be directly emitted to the console).
Is there away to do that?
It turns out that I can achieve this but just making the child process inherit the stdio. ex:-
const { spawn } = require('child_process')
const shell = spawn('sh',[], { stdio: 'inherit' })
shell.on('close',(code)=>{console.log('[shell] terminated :',code)})
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.
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.
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.