This is a similar question to here. However I am using Ubuntu and the previous question's accepted answer does not seem relevant.
I am using node to call a shell script that in turn calls chrome. A terminal opens and echo's the url but chrome browser does not open. I have also tried /usr/bin/google-chrome after discovering it from which command as well as google-chrome-stable to no avail. Why doesn't chrome launch on Ubuntu with node.js child process? Im running desktop version 16.04 LTS. If I run this shell script on the terminal without node it runs great.
JS:
var exec = require('child_process').exec,
child;
child = exec('gnome-terminal -x '+__dirname+'/ss.sh http://www.google.com',
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
SHELL (ss.sh)
#!/bin/bash
echo $1
google-chrome $1 --start-maximized
OUTPUT:
Edit: I just tried running this on another box running Ubuntu 14.04 and receive the error: failed to create /home/user/.pki/nssdb directory. The plot thickens.
JAVA: If I run this with almost the same code in Java it works perfectly:
public static void main(String[] args) {
try {
String url = args[0];
ProcessBuilder pb = new ProcessBuilder("/home/user/ss.sh", url);
pb.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The answer to this was file permission and it came from #paulsm4 comment. He references #A.B. answer from here.
As soon as he posts his comment as an answer I will accept and delete this.
Related
I'm a newbie to nodejs and currently working to run a certain cpp program running on a nodejs server with a click on a button.
After googling for a while, I've found that I can use something called 'Child process' to execute cpp program from a server side. And I've tried to include the following code in 'index.js' in '.nuxt' folder.
const { exec, spawn } = require("child_process");
exec('"C:\\Projects\\Monitoring_and_Diagnosis\\socketgraph\\build\\" spectrum_sim_cu', (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
const child = spawn("./spectrum_sim_cu"); //where a is the exe file generated on compiling the code.
child.stdin.write("-10 10 (5/260) 70.03 0");
child.stdin.end();
child.stdout.on("data", (data) => {
console.log(`child stdout:\n${data}`);
});
});
I thought 'index.js' would be the right place to insert the above code since my cpp program needs to run and connect with the server as the nodejs server starts up. However, the whole '.nuxt' folder seems to be refreshed when I type 'npm run dev' to run the server.
Is it the right place or approach to run the cpp program on nodejs server.
Can anyone help me out please?
Thanks in advance.
I need an option to open user's default web browser from node js app which is running inside docker container. I need to know that to implement OAuth flow.
I know that I can do it by opening new tab on the client side, but I don't have this option for other reasons.
I'm not familiar with docker container, but for just straight nodejs, this works for me:
// start browser
if (opSys == "Win64")
command = ("start http://localhost:8000/init"); // Win64
else if (opSys == "MacOS")
command = ("open http://localhost:8000/init"); // MacOS
else
command = ("xdg-open http://localhost:8000/init"); // Linux
exec (command, function (error, stdout, stderr)
{
if (error)
{
console.log ("command: ", command);
console.log ("error: ", stderr);
}
});
I am running nodejs child_process function spawn() to do mongoexport. I have passed all the necessary fields to the command and it is working fine on my local machine. Below is the function code
userDetailsChild = spawn('mongoexport', ['--username',username,'--
password',password,'--db',db,'--collection','users','--type', 'csv',
'--fields', 'userId,firstName','--
out','/home/bitnami/apps/webapp/dist/server/prod/public/user-
details.csv']);
userDetailsChild.on('exit', function (code: any) {
if (code != 0) {
userDetailsChild.kill();
callback(new Error(), null);
} else {
console.log('userDetailsChild process closed with code ' + code);
userDetailsChild.kill();
}
});
When I try to run the same code with server credentials on amazon WordPress ec2 instance on my server machine it fails with no error message.
Somewhere I have seen path variables set as process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];
But didnt worked in my case.
Any help will be appreciated!!
While I do not know why the command is failing, I do know that you appear to be ignoring the more than likely helpful information coming back to you via stderr. ;)
I have the following code (copied from the node docs apart from the command itself) :
var util = require('util'),
exec = require('child_process').exec,
child,
command = 'libreoffice --headless -convert-to pdf mysourcefile.doc -outdir /tmp';
child = exec(command,
function (error, stdout, stderr) {
if (error !== null) {
console.log(error);
return;
}
);
The command appears to be executing fine (output file is there) but error is always "Error: Command failed:" and err is not defined (the docs say err.code will give more information).
What am I doing wrong / overlooking?
It should be error.code.
The docs mix the use of error and err; it refers to the Error object provided to the callback.
like i say . years after. i got the same error. just find what can be the error - checkout (https://stackoverflow.com/a/21137820/1211174). if you are on windows there is a chance that you have someauto run on cmd. and then this autorun failed. and you get both output. sterr and stdout
Can Node.js launch scripts on the server it is installed on? Scripts like bash scripts or PHP scripts, for example to resize pictures?
If so, how how is it? Can you point me to a documentation page, please?
Thanks,
Dan
Yes it is possible. The following give a demonstration:
http://www.dzone.com/snippets/execute-unix-command-nodejs
http://groups.google.com/group/nodejs/browse_thread/thread/e3d00bb0e48dd760?pli=1
You can also perform tasks such as spawning child processes, and clustering.
Executing a unix command:
var exec = require('child_process').exec;
child = exec("something", function (error, stdout, stderr) {
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
See Node.js Documentation for more: http://nodejs.org/api/child_process.html