phantomjs child process execution under cygwin - cygwin

I'm trying to call shell script from casperjs script under cygwin environment. Code below prints output if I use system commands like "ls, pwd, find...". But when I specify custom.sh script inside execFile function it prints nothing. And it looks like execFile doesn't trigger shell script.
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
});
var cp = require('child_process');
casper.start();
casper.then(function(){
cp.execFile('custom.sh', 'arg', {}, function (err, stdout, stderr) {
console.log("execFileSTDOUT:", JSON.stringify(stdout));
console.log("execFileSTDERR:", JSON.stringify(stderr));
});
casper.waitFor(function check () {
return false;
}, function then() {
console.log('Success').exit();
}, function timeout() {
this.echo("Timeout!!!").exit();
}, 3000)
});
casper.run();
custom.sh:
#!/bin/sh
echo >&2 some message
echo "code";
echo "xxx" > trigger.log
custom.sh prints output if I run it directly from command line:
$ custom.sh
some message
code
But there is nothing in casper log:
$ casperjs test.js
[info] [phantom] Starting...
[info] [phantom] Running suite: 1 step
[debug] [phantom] Successfully injected Casper client-side utilities
execFileSTDOUT: ""
execFileSTDERR: ""
[info] [phantom] Step anonymous 1/1: done in 38ms.
[info] [phantom] Step _step 2/2: done in 50ms.
I tried to put custom.sh inside /usr/bin and specify absolute path but without success.
EDIT:
One point to mention
cp.execFile('/usr/bin/ls', '/' - returns nothing
cp.execFile('ls', '/' - outputs files listing

It seems the problem is related to cygwin path parameters.
Finally function call below works as expected:
cp.execFile('sh', 'custom.sh' ...

Related

nodejs run a bash script using spawn

I know this question has been asked many times but I have tried most of the methods and they just don't work for me.
So here is my problem, I have a simple bash script like this
#!/bin/bash
echo "Username: $1";
echo %DATABASE_URL%;
I want to run this script in a separate process. so if the parent process gets killed during my script being excused it still continues running.
Here is my nodejs code
const child = spawn('bash', [`script.sh`, 'test'], {
detached: true,
cwd: process.cwd(),
detached: true,
stdio: "inherit",
DATABASE_URL: 'test'
}, function (err, stdout, stderr) {
// Node.js does not invoke this
console.log(stdout);
stdout.on("data", data => {
console.log('Output of script execution');
});
stderr.on("data", data => {
console.log('an error with file system');
});
});
child.unref();
child.on('exit', (code) => {
console.log("Child exited");
});
So I know that my script returns some output and should run callback but it does not run it. It directly jumps to the on.('exit') callback which confuses me.
Also it worth mentioning that I am testing the code on windows and bash script.sh 'test' works if I run it on cmd.
Posts I have tried:
How to run shell script file using nodejs?
Execute script from Node in a separate process
Bash Script : what does #!/bin/bash mean?
and many of the existing weblogs that explains the same.

Is it possible to monitor terminal for specific output?

I am using NodeJS to run webots by command line and have redirected the stdout too the node terminal. My problem is that I want to trigger an event based on a console log. I tried redirecting the stdout of the command to another file, but this didn't seem to work.
This is the console output
INFO: sumo_example_two: Starting controller: python.exe -u sumo_example_two.py
INFO: sumo_supervisor: Starting controller: python.exe -u sumo_supervisor.py
robot2
INFO: sumo_example_one: Terminating.
INFO: sumo_example_two: Terminating.
INFO: sumo_supervisor: Terminating.
stdout:
I want to extract 'robot2'.
I have just tested and the following snippet works fine for me:
const { spawn } = require('child_process');
const ls = spawn('webots', ['--stdout']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
// Process `data` as you prefer, something like
//
// if (data.includes('robot2')) {
// something()
// }
});

PM2 breaks child process bash script

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

how to run node shelljs in sync mode and get stdout and stderr

Within a nodejs script I have the following code which makes the call synchronously and returns the stdout from the shell script I am calling:
var sh = require('shelljs');
... some code
var output = sh.exec('./someshellscript.sh', {silent:true}).stdout;
console.log(output);
... some more code (that shouldnt run until the script is complete)
I can also run the following script which will instead return the stderr:
var sh = require('shelljs');
... some code
var output = sh.exec('./someshellscript.sh', {silent:true}).stderr;
console.log(output);
... some more code (that shouldnt run until the script is complete)
However I want to receive both stdout and stderr in a sync call. Its probably something pretty obvious I am missing herebut I cant work it out.
I think you used to be able run the following command in previous versions but this just returns undefined now:
var sh = require('shelljs');
... some code
var output = sh.exec('./someshellscript.sh', {silent:true}).output;
console.log(output);
... some more code (that shouldnt run until the script is complete)
Relevant software versions are:
Ubuntu: 14.04.3 LTS
node: 4.4.4
npm: 2.15.1
shelljs: 0.7.0
Any help appreciated thanks.
From the README for the method exec(command [, options] [, callback])
Executes the given command synchronously, unless otherwise specified. [...], returns an object of the form { code:..., stdout:... , stderr:... }).
Therefore
const { stdout, stderr, code } = sh.exec('./someshellscript.sh', { silent: true })

Using bash with Node.js child_process using the shell option fails

The child process api can be used to execute shell script in node.js.
Im using the child_process.exec(command[, options], callback) function
as an option the user of exec can set the shell: '/path/to/shell' field to select the shell to be used. (Defaults to '/bin/sh')
Setting options to {shell: '/bin/bash'} does not make exec runt the command with bash.
I have verified this by issuing the command "echo $0" which prints "/bin/sh".
How can I use bash with child_process.exec through the shell option?
(My goal is to make use of my path definitions in bashrc, now when i try to use grunt the binary cannot be found. Setting the cwd, current working directory in the options dictionary works as expected)
----------------- UPDATE, example
'use strict';
var cp = require('child_process');
cp.exec('echo $0', { shell: '/bin/bash' }, function(err, stdout, stderr){
if(err){
console.log(err);
console.log(stderr);
}
console.log(stdout);
});
Output:
/bin/sh
which bash
prints:
/bin/bash
Might be in your setup or passing options incorrectly in your code. Since you didn't post your code, it's tricky to tell. But I was able to do the following and it worked (using node 4.1.1):
"use strict";
const exec = require('child_process').exec
let child = exec('time',{shell: '/bin/bash'}, (err, stdout, stderr) => {
console.log('this is with bash',stdout, stderr)
})
let child2 = exec('time',{shell: '/bin/sh'}, (err, stdout, stderr) => {
console.log('This is with sh', stdout, stderr)
})
The output will be:
this is with bash
real 0m0.000s
user 0m0.000s
sys 0m0.000s
This is with sh /bin/sh: 1: time: not found
I used time as the command since it's one that bash has and sh does not. I hope this helps!

Resources