当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - nodejs spawn "su user -c 'ls -la'" not working

问题描述:

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'
];
分享给朋友:
您可能感兴趣的文章:
随机阅读: