简体   繁体   English

从node.js调用sh脚本

[英]Calling a sh script from node.js

I have a node.js application, which connect everyday to a server. 我有一个node.js应用程序,它每天都连接到服务器。

On this server, a new version of the app can be available, if so, the installed app download it, check if the download is complete, and if so, stop itself calling a shell script, which replace the old app by the new one, and start it. 在此服务器上,可以使用该应用程序的新版本,如果可以,则已安装的应用程序将其下载,检查下载是否完成,如果已完成,请停止调用外壳程序脚本,该脚本将新应用程序替换旧应用程序,然后启动它。

I m struggling at starting the update script. 我正在努力启动更新脚本。

I know I can start it with child_process_execFile function, which I do: 我知道我可以使用child_process_execFile函数启动它,我可以这样做:

var execF = require('child_process').execFile;
var PATH = process.argv[1].substr(0, process.argv[1].lastIndexOf('/')+1),
    filename = 'newapp.js',
execF(PATH + 'up.sh', [PATH + filename], function () {console.log('done'); return ;});

up.sh, for now is just: up.sh,现在仅是:

cat $1 > /home/pi/test

I get 'done' printed in the console, but test isn t created. 我在控制台中打印了“完成”,但未创建测试。

I know that execFile create a subprocess, is it what block the script to do that? 我知道execFile创建一个子进程,是什么阻止了脚本的执行?

If I suceed to start this, I know I only have to make some cp in the script to have my app auto-updating. 如果成功启动此程序,我知道我只需在脚本中添加一些cp即可自动更新我的应用。

EDIT: 编辑:

Started as usual (calling the script from console), it work well, is there a reason for the script to don t execute when called from node.js? 像往常一样启动(从控制台调用脚本),它运行良好,是否有理由从node.js调用时不执行脚本?

I'd suggest that you consider using a module that can do this for you automatically rather than duplicating the effort. 我建议您考虑使用一个可以自动为您完成此任务的模块,而不是重复工作。 Or, at least use their technique as inspiration for you own requirements. 或者,至少利用他们的技术来启发您自己的需求。

One example is: https://github.com/edwardhotchkiss/always 一个示例是: https : //github.com/edwardhotchkiss/always

It's simple to use: 使用简单:

Usage: always <app.js>
=> always app.js

Then, anytime your code changes, the app is killed, and restarted. 然后,只要您的代码发生更改,该应用就会被杀死,然后重新启动。

As you can see in the source, it uses the Monitor class to watch a specified file, and then uses spawn to kick it off (and of course kill to end the process when a change has happened). 正如您在源代码中看到的那样,它使用Monitor类监视指定的文件,然后使用spawn来启动它(当然,当发生更改时,使用kill方法终止该过程)。

Unfortunately, the [always] output is currently hardcoded into the code, but it would be a simple change/pull request I'm sure to make it optional/configurable. 不幸的是, [always]输出当前已被硬编码到代码中,但这将是一个简单的更改/拉取请求,我敢保证使其为可选/可配置。 If the author doesn't accept your change, you could just modify a local copy of the code (as it's quite simple overall). 如果作者不接受您的更改,则可以修改代码的本地副本(因为总体上这很简单)。

Make sure when you spawn/exec the process you are executing the shell that will be processing the script and not the script itself. 确保在生成/执行进程时,正在执行将要处理脚本而不是脚本本身的外壳程序。

Should be something like 应该是这样的

execF("/usr/bin/sh", [PATH + 'up.sh', PATH + filename]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM