2

I have an little problem here. I have nodejs app that should run command in OS's bash with root rights, f.e.

Command is: echo "$password" | /usr/bin/sudo /usr/bin/abc --key "$username"

Here is my code:

const spawn = require('child_process').spawn;
function sendMessage() {
    let username = 'WhoLetTheDogsOut';
    let password = 'Woof!';
    const echo = spawn('echo', [ password ]);
    const abc = spawn('sudo', [ `/usr/bin/abc --key ${username}` ]);
}
sendMessage();

Please, help. I didn't get where I'm wrong after reading official Node child_process.spawn() doc and I'm keep getting error: gnokii stderr: sudo: /usr/bin/gnokii --sendsms +375293941196: command not found

6
  • Do you mean to have ` around /usr/bin/abc --key ${username}? Shouldn't they be '. Commented Sep 28, 2016 at 16:01
  • @phreed template string literals Commented Sep 28, 2016 at 16:07
  • Do you have gnokii gnokii.org/index.shtml installed on your system? Commented Sep 28, 2016 at 16:20
  • I do not see where you are tying the std-output of 'echo' to the std-input of 'abc'. Commented Sep 28, 2016 at 16:32
  • 1
    ok, I think what is happening is that one of the spawned processes is failing (probably abc as the password is not arriving) and then the gnokii is failing for some other unrelated reason. You need to tie the stdout from echo to the stdin of abc. You should also write out the errors from both echo and abc and post the results. echo.stdout.on('data', (data) => { abc.stdin.write(data); }); Commented Sep 28, 2016 at 17:09

1 Answer 1

3

I hope this will help you , it's a package of NPM "node-cmd", you can run commands in bash with nodejs, Here is an example:

var cmd=require('node-cmd');

cmd.get('echo '+password+' | /usr/bin/sudo /usr/bin/abc --key '+username,
    function(data){
        console.log('The result of the command:'+data)
    }
);

Remember give permisions (chmod) to the file and run the process like root if you need.

This is the source https://www.npmjs.com/package/node-cmd

Sign up to request clarification or add additional context in comments.

1 Comment

I'll try it and post result later. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.