35

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

childProcess.exec('~/./play.sh /media/external/' + req.params.movie, 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)

Ralf
  • 836
  • 1
  • 9
  • 32
  • 1
    Are there any [`'error'`s](http://nodejs.org/api/child_process.html#child_process_event_error) or does it output any [`'data'`](http://nodejs.org/api/stream.html#stream_event_data) to [`stdout`](http://nodejs.org/api/child_process.html#child_process_child_stdout) or [`stderr`](http://nodejs.org/api/child_process.html#child_process_child_stderr)? – Jonathan Lonowski Sep 30 '13 at 21:02
  • /bin/sh: 1: /root/./play.sh: not found, How I have to modify my command to run this script stored in home dir? – Ralf Sep 30 '13 at 21:16

2 Answers2

60

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('~/./play.sh /media/external/' + req.params.movie,
  function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
      console.log('exec error: ' + error);
    }
});
Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
smokey.edgy
  • 636
  • 6
  • 3
  • /bin/sh: 1: /root/./play.sh: not found, How I have to modify my command to run this script stored in home dir? – Ralf Sep 30 '13 at 21:23
  • Thank you, found it out, it was just the incorrect path. (I think I have to go to bed (23:39 UTC+1)) – Ralf Sep 30 '13 at 21:30
  • is `exec('~/./play.sh /media/external/' + req.params.movie ...` a safe construct? – Yaur May 22 '17 at 20:36
5
exec('sh ~/play.sh /media/external/' + req.params.movie ,function(err,stdout,stderr){
      console.log(err,stdout,stderr);
 })

Runs your play.sh shellscript with /media/external/+req.params.movie as argument. The output is available through stdout,stderr variables in the callback.

OR TRY THIS

var myscript = exec('sh ~/play.sh /media/external/' + req.params.movie);
myscript.stdout.on('data',function(data){
    console.log(data); // process output will be displayed here
});
myscript.stderr.on('data',function(data){
    console.log(data); // process error output will be displayed here
});`
n_rao
  • 155
  • 1
  • 5