使用 node.js (子进程) 运行 shell 脚本
Run shell script with node.js (childProcess)
我想在我的节点.js服务器上运行一个 shell 脚本,但什么也没发生......
childProcess.exec('~/./play.sh /media/external/' + req.params.movie, function() {}); //not working
另一个子进程工作完美,但上面的过程不会。
childProcess.exec('ls /media/external/', movieCallback); //works
如果我在终端中运行脚本,那么它可以工作。有什么想法吗?(设置了 chmod +x)
exec 函数回调有 error、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);
}
});
exec('sh ~/play.sh /media/external/' + req.params.movie ,function(err,stdout,stderr){
console.log(err,stdout,stderr);
})
运行你的play.sh
shellscript,/media/external/
+req.params.movie作为参数。输出可通过回调中的 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
});`
相关文章:
- 如何使用phaser使html5游戏在移动设备浏览器上运行
- 使用压缩的JavaScript文件(不是运行时压缩)
- Javascript运行php文件,然后下载文件
- 如何在 JavaScript 中运行 shell 命令
- 代码在shell中运行,但提供'可以't查找变量'从文件运行时出错
- 如何在 Atom-Shell 中运行 reload() 和 open()
- 在 Javascript 中运行命令 Wscript.shell ActiveX
- 找不到从 CLI 运行 PHP 的 grunt-shell 任务
- 运行FTP(JavaScript中的命令shell)服务器端
- 使用 node.js (子进程) 运行 shell 脚本
- 如何使用 shell 脚本文件运行我的节点脚本
- node.js中的execSync;无法正确运行shell命令
- Gnome shell扩展:如何使用管道运行命令
- 运行mongoDB shell的JavaScript引擎是什么?
- Nodejs ui页面是否可以运行shell cmd到远程机器并运行脚本
- 显示从Jscript运行的shell命令的输出
- 为mongo shell运行js文件
- Electron (Atom-Shell)从页面中的链接运行unix命令
- ActiveXObject运行不返回(shell)
- 有没有任何非Windows方式可以从本地.html文件运行shell命令