【问题标题】:Run shell script with node.js (childProcess)使用 node.js (childProcess) 运行 shell 脚本
【发布时间】:2013-10-06 21:04:16
【问题描述】:

我想在我的 node.js 服务器上运行一个 shell 脚本,但是什么也没发生...

childProcess.exec('~/./play.sh /media/external/' + req.params.movie, function() {}); //not working

另一个 childProcess 可以完美运行,但上面的过程不会。

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

如果我在终端中运行脚本,那么它可以工作。有任何想法吗? (chmod +x 已设置)

【问题讨论】:

  • 是否有'error's 或者它是否输出任何'data'stdoutstderr
  • /bin/sh: 1: /root/./play.sh: 未找到,我如何修改我的命令来运行存储在主目录中的这个脚本?

标签: javascript node.js debian raspberry-pi raspbian


【解决方案1】:

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

【讨论】:

  • /bin/sh: 1: /root/./play.sh: 未找到,我如何修改我的命令来运行存储在主目录中的这个脚本?
  • 谢谢,找到了,只是路径不对。 (我想我得睡觉了(23:39 UTC+1))
  • exec('~/./play.sh /media/external/' + req.params.movie ... 是一个安全的构造吗?
【解决方案2】:
exec('sh ~/play.sh /media/external/' + req.params.movie ,function(err,stdout,stderr){
      console.log(err,stdout,stderr);
 })

/media/external/+req.params.movi​​e 作为参数运行你的play.sh shellscript。输出可通过回调中的 stdout、stderr 变量获得。

或试试这个

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

【讨论】:

  • 请在您的答案中添加解释
猜你喜欢
  • 1970-01-01
  • 2020-06-16
  • 2016-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-08
  • 1970-01-01
相关资源
最近更新 更多