It seems like I can't access the $HOME property within my nodeJS app when running a child_process on my Linux machine:
var exec = require('child_process').exec
var testHome = `echo $HOME`
testHomeCmd = exec(testHome)
testHomeCmd.stdout.on('data', function (data) {
console.log(data)
})
Where the output I receive is: [17597]: /
If I run echo $HOME in terminal I receive:
$ echo $HOME
/home/cs4
Any thoughts?
Related
Instead of specifying root of file for execution in a remote server, how we can use cd command in exec() in nodejs.What i am done is like this.
var command_part1 = `ssh -p 22 root#ip`;
var command_part2 = `python3 test.py ${input}`;
var folderPath = `/root/folder/`;
var child = exec(`${command_part1} && cd ${folderPath} && ${command_part2}` , function (error, stdout, stderr) {
if (error !== null) {
callback(error)
} else {
callback();
}
});
But this code is not working, when i am executing this command locally,ssh login only is happening, further commands are not working.How can i make this working ? I dont want to specify the folder path like python3 /root/folder/test.py ${input};
Thank You Inadvance
Try this code,
`var command = `sshpass -p givePassword ssh -o "StrictHostKeyChecking no" root#ip "cd ${folderPath} && ${command_part2}"`
sshpass is a simple and lightweight command line tool that enables us to provide password (non-interactive password authentication) to the command prompt itself, so that automated shell scripts can be executed
In my electron/reactjs app, i'm trying to open a terminal and launch somes commands.
My code looks like this :
const terminal = 'x-terminal-emulator';
const { spawn } = require('child_process');
spawn(terminal);
My terminal opens but i don't know how to launch commands in this terminal like 'cd /my/custom/path && ls'
Can someone help me please ? :)
Node.js child_process.spawn command have an option to specify the shell you want to use.
So I would use the opposite logic and launch directly the command within a particular shell (for exemple bash):
const { spawn } = require('child_process');
const terminal = '/bin/bash';
let cmd = 'echo $SHELL';
spawn(cmd, { shell: terminal })
.stdout.on('data', (data) => {
console.log(`stdout: ${data}`); //-> stdout: /bin/bash
});
I have a place in my code where I need to spawn a bash script, write to its standard input, and read from its standard output.
I do this using node's child_process.spawn.
Unfortunately, when I run this code under pm2, the bash script hangs forever when calling mkdir.
Is there any way to avoid this issue?
test.js
'use strict';
const child_process = require('child_process');
setInterval(() => {
const process = child_process.spawn('./test.sh');
process.on('exit', () => {
console.log('process exit');
});
process.stdout.on('data', (data) => {
console.log('Output: ' + data.toString('utf8'));
});
}, 1000);
test.sh
#!/usr/bin/env bash
TEMP=$(mktemp -d);
echo "Created directory $TEMP"
rm -rf ${TEMP}
echo "Deleted directory $TEMP"
Expected output
Starting
Spawning test.sh
Output: Created directory /tmp/tmp.I6Buifdmlu
Output: Deleted directory /tmp/tmp.I6Buifdmlu
process exit
Actual output
[STREAMING] Now streaming realtime logs for [test] process
2|test | Spawning test.sh
2|test | Spawning test.sh
Environment
OS: Ubuntu 14.04
Node: 4.7.3
PM2: 2.4.0
NB: I have tested this on Mac OSX and there is no problem
I'm trying to spawn a process in NodeJS that accesses the my home folder, and can't see, to get either of the options below to work.
var spawn = require('child_process').spawn,
options = {stdio: 'inherit', env: process.env};
spawn('ls', ['~/'], options);
spawn('ls', ['$HOME'], options);
Output
ls: ~/: No such file or directory
ls: $HOME: No such file or directory
I've verified that options.env.HOME is properly set, any idea what I'm doing wrong?
Update
So this is what I ended up doing to make my use-case work (using script instead of ls):
spawn('script', [process.env.HOME], options);
Then, inside of my script:
#!/usr/bin/env bash
export HOME=$1
I still don't understand why options.env.HOME does not seem to work as expected.
process.env.HOME is what you want. Use it like so:
var spawn = require('child_process').spawn,
options = {stdio: 'inherit'};
var ls = spawn('ls', [process.env.HOME]);
ls.stdout.on('data', function(data){
console.log(String(data));
});
ls.stderr.on('data', function(data){
console.log(String(data));
});
Then you can set HOME in your shell when invoking the node script:
HOME='/tmp'; node ls.js
Alternatively, you don't have to overload HOME. To use whatever variable you like export it first, and then access it through process.env.:
export FOO='/tmp'; node ls.js
Not sure why this does not work, if I run a simple command such as cmd='ls -all' then I get the output back, but when I use this to run a command which takes some time to complete I don't get anything at all returned.
In this example, I am using lftp to mirror some folders and want to get the reply, if I run the command from the terminal then of course I see the output, but using child process I get nothing:
var childProcess = require('child_process');
var cmd = 'lftp sftp://user:password#somehost -e "mirror -R --delete --parallel=5 /usr/share/scripts/ /volumes/folders/usr/share/;bye"';
childProcess.exec(cmd, function (error, stdout, stderr) {
console.log('stdout:'+stdout);
console.log('stderr:'+stderr);
console.log('error:'+error);
});
I also tried the spawn method, nothing returned from that either:
var spawn = require('child_process').spawn;
var lftp = spawn('lftp',['sftp://user:password#somehost', '-e "mirror -R --delete --parallel=5 /usr/share/scripts/ /volumes/folders/usr/share/;bye"']);
lftp.stdout.on('data', function(data) {
console.log(data.toString());
});
I think the problem is that lftp buffers its output.
To get around that you'll need to use unbuffer (http://expect.sourceforge.net/example/unbuffer.man.html) to send the output directly to stdout.