从shell脚本中杀死Node.js程序

时间:2021-07-08 21:59:45

I'm running a node.js application which is acting as a man-in-the-middle proxy to proxy all of the requests I'm making through a PhantomJS headless testing environment. I want to spin up this proxy from either my PhantomJS test script (though I've looked into it and it seems that phantom does not have an exec() command for executing shell commands) or a small shell script which manages both processes. Ideally, it would do something like

我正在运行一个node.js应用程序,该应用程序充当中间人代理,代理我通过PhantomJS无头测试环境发出的所有请求。我想从我的PhantomJS测试脚本中调出这个代理(虽然我已经调查过它,似乎幻像没有执行shell命令的exec()命令)或者是一个管理这两个进程的小shell脚本。理想情况下,它会做类似的事情

#!/bin/bash
node proxy.js
phantomjs runTests.js
kill node process here

Is there any way that I can do this?

有什么方法可以做到这一点吗?

1 个解决方案

#1


3  

Moments after asking this question, I found a much better way to execute the phantom program from within my node app by using a child process. I placed my phantom script within the folder which contained my node app, and then used exec like this:

在提出这个问题之后,我发现了一个更好的方法,通过使用子进程从我的节点应用程序中执行幻像程序。我将我的幻像脚本放在包含我的节点应用程序的文件夹中,然后像这样使用exec:

var exec = require('child_process').exec;
var _phantom = exec('phantomjs runTests.js',function(error,stdout,stderr){
    console.log(stdout);
};

_phantom.on('exit',function(code,sig){
    process.exit(code);
});

This means I could spin up my proxy server, then execute the child process. The _phantom.on('exit') block allows me to detect when the process exits on a code. Then, it's very simple to signal the node app to quit, using process.exit.

这意味着我可以启动我的代理服务器,然后执行子进程。 _phantom.on('exit')块允许我检测进程何时退出代码。然后,使用process.exit向节点应用程序发出退出信号非常简单。

#1


3  

Moments after asking this question, I found a much better way to execute the phantom program from within my node app by using a child process. I placed my phantom script within the folder which contained my node app, and then used exec like this:

在提出这个问题之后,我发现了一个更好的方法,通过使用子进程从我的节点应用程序中执行幻像程序。我将我的幻像脚本放在包含我的节点应用程序的文件夹中,然后像这样使用exec:

var exec = require('child_process').exec;
var _phantom = exec('phantomjs runTests.js',function(error,stdout,stderr){
    console.log(stdout);
};

_phantom.on('exit',function(code,sig){
    process.exit(code);
});

This means I could spin up my proxy server, then execute the child process. The _phantom.on('exit') block allows me to detect when the process exits on a code. Then, it's very simple to signal the node app to quit, using process.exit.

这意味着我可以启动我的代理服务器,然后执行子进程。 _phantom.on('exit')块允许我检测进程何时退出代码。然后,使用process.exit向节点应用程序发出退出信号非常简单。