问题描述:

I can not spawn a simple command line: su username -c "ls -la"

here is my code:

var childArgs = [

'username',

'-c',

'"ls -la"'

];

var ph = spawn('su', childArgs);

ph.stdout.on('data', function (data) {

console.log(data);

});

ph.stderr.on('data', function (data) {

console.log('stderr___' + data);

});

ph.on('close', function (code) {

console.log('close__' + code);

});

ph.on('error', function (error) {

console.log('error___' + error);

});

output:

stderr___bash: ls -la: command not found

is there any way to spawn this command via nodejs ?

网友答案:

Split your "ls -la" into two items in the array.

var childArgs = [
  'username',
  '-c',
  'ls',
  '-la'
];
网友答案:

it works only if I remove double quotes

var childArgs = [
    'spawnuser',
    '-c',
    'ls -la'
];
相关阅读:
Top