I am trying to run an async node child_process which runs a docker container.
let runDockerCmd = `docker-compose -f ./vm/docker-compose.yml run --rm vm node temp.js`;
child_process.exec(runDockerCmd, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
Where temp.js contains a for loop. Sometimes it runs half the loop, sometimes a third etc. then it terminates.
I can get it to work using child_process.execSync but that is a bit unsavoury. Why does it exit like that, and how do I keep it alive?
Related
I have an executable file that outputs every few seconds of data once i run it via the Terminal on Mac OS.
How can i get the data using node js?
Thank you.
You can run the command, which you run in the terminal, as a Node.js child process using spawn or exec.
Example:
const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
The more information you can find here
I try to unpack img file with 7z on Linux. In terminal it works fine:
7z x myimg.img
But when I try to do this from node.js script it fails:
const childProcess = require('child_process');
childProcess.exec('7z x myimg.img', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
I get this message:
exec error: Error: Command failed: 7z x myimg.img
ERROR: myimg.img
Can not open the file as archive
If I run command whereis 7z I get the same path.
I've scheduled a node script in crontab to run after reboot by adding this line to my /etc/crontab:
#reboot /home/eugen/Documents/scripts/tests/run_after_boot/index.js
/home/eugen/Documents/scripts/tests/run_after_boot/index.js file contains this script, which is supposed to open vscode:
const { exec } = require('child_process');
exec('code', (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err)
} else {
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
}
});
After reboot, nothing happens. Also, I tried to add the same line (#reboot /home/eugen/Documents/scripts/tests/run_after_boot/index.js) by executing the crontab -e command in my terminal, but the same problem appeared: nothing happened after reboot.
I created a micro-service using the "Moleculer" framework. I used child_process.exec for running docker commands in a shell. I get "/bin/sh: 1: docker: not found" error.
const { exec } = require("child_process");
.
.
.
exec("docker --version", (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
Tried giving absolute path as:
exec("/usr/bin/docker --version", (err...);
But still got the same error.
I am able to get the node version using:
exec("node --version", (err...);
I just had the same problem. The reason was simply that I had built a new image but the Docker client was not present in my container because I only did docker-compose restart <CONTAINER_NAME> and not docker-compose stop <CONTAINER_NAME>, docker-compose rm <CONTAINER_NAME> and docker-compose up -d <CONTAINER_NAME>.
https://stackoverflow.com/a/63281470/1707015
I want to run npm install via typescript code in a specified directory.
I found this code:
npm.load({}, function(err: any) {
// handle errors
// install module ffi
npm.commands.install(["hello-world#0.0.1"], function(err: any, data: any) {
// log errors or data
});
npm.on('log', function(message: any) {
// log installation progress
console.log(message);
});
});
But now I don't want to install hello-world, but just run npm install (without any package).
Additionally it should run in a path that I can specify, like ./folder/subfolder
How can I do that?
Apart from exec it's also possible to use the npm package:
import * as cp from 'child_process';
var npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
var path = '/path_to_npm_install';
const result = cp.spawnSync( npm, ['install'], {
cwd: path
});
If you're using Nodejs, which I think you are, you can run
child_process.exec('npm install') // or any other command which you give from terminal or command prompt
Check the documentation for child_process
https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
You can create a nodejs script that expect the directory path from user and create a child process and execute that command in that.
index.js
const { exec } = require('child_process');
exec(`cd /${process.env.PATH} | npm install`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
PATH=/path_of_directory_to_run_npm_install node index.js
Read more about child_process from nodejs documentation - https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback