Run shell script with node.js (childProcess)

Posted on

Run shell script with node.js (childProcess)

I want to run a shell script on my node.js server, but nothing happened…

childProcess.exec('~/./ /media/external/' +, function() {}); //not working

Another childProcess works perfect, but the process above won’t.

childProcess.exec('ls /media/external/', movieCallback); //works

If I run the script in terminal, then it works. Any ideas? (chmod +x is set)

Solution :

The exec function callback has error, stdout and stderr arguments passed to it. See if they can help you diagnose the problem by spitting them out to the console:

exec('~/./ /media/external/' +,
  function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
      console.log('exec error: ' + error);

exec('sh ~/ /media/external/' + ,function(err,stdout,stderr){

Runs your shellscript with /media/external/ as argument. The output is available through stdout,stderr variables in the callback.


var myscript = exec('sh ~/ /media/external/' +;
    console.log(data); // process output will be displayed here
    console.log(data); // process error output will be displayed here

Leave a Reply

Your email address will not be published. Required fields are marked *