Search code examples
javascriptnode.jsbashsudospawn

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 ?


Solution

  • it works only if I remove double quotes

    var childArgs = [
        'spawnuser',
        '-c',
        'ls -la'
    ];