使用node运行shell脚本。js(childProcess)

时间:2022-06-01 21:42:28

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

我想在我的节点上运行shell脚本。js服务器,但是什么都没发生……

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)

如果我在终端上运行脚本,那么它就会工作。什么好主意吗?(chmod + x是集)

2 个解决方案

#1


57  

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函数回调具有传递给它的错误、stdout和stderr参数。看看他们是否能帮你诊断出问题,把他们吐到控制台:

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);
    }
});

#2


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.

运行你的游戏。sh shellscript /媒体/外部/ + req.params。电影作为参数。输出可以通过stdout、stderr变量在回调中获得。

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
});`

#1


57  

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函数回调具有传递给它的错误、stdout和stderr参数。看看他们是否能帮你诊断出问题,把他们吐到控制台:

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);
    }
});

#2


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.

运行你的游戏。sh shellscript /媒体/外部/ + req.params。电影作为参数。输出可以通过stdout、stderr变量在回调中获得。

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
});`