从 Nodejs 应用程序 (Linux) 中重新启动外部进程

发布于 2025-01-19 02:11:30 字数 539 浏览 8 评论 0原文

假设我正在从命令行(而不是nodejs应用程序)运行一个进程:

myProcess doSomething --withParam1

我还在运行连接到此过程(RPC)的Nodejs应用程序。

node myApp

myProcess将随机静止地无法正常工作而不会崩溃,而myApp将检测到它。我需要myApp才能重新启动myProcess(杀死并重新开始)。重新启动myProcess之后,myApp也将使用pm2重新启动(我已经在处理Nodejs App的PM2重新启动零件 - 我的问题是我无法使用PM2重新启动myProcess因为它不是nodejs应用程序)。另外,由于它是第三方软件,因此我无法更改myProcess的代码。

如何从nodejs应用程序重新启动外部进程?

Say that I am running a process from the command line (NOT a nodejs app):

myProcess doSomething --withParam1

I am also running a nodejs app that connects to this process (rpc).

node myApp

myProcess will randomly silently fail to work properly without crashing and myApp will detect it. I need myApp to be able to restart myProcess (kill it and start it again). After I restarted myProcess, myApp will also restart itself using pm2 (I am already handling the pm2 restart part for the nodejs app - my issue is that I cannot use pm2 to restart myProcess since it is not a nodejs app). Also, I cannot change the code of myProcess since it is a 3rd party software.

How can I restart the external process from my nodejs app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一腔孤↑勇 2025-01-26 02:11:30

我最终使用 process.kill.kill =“ https://nodejs.org/api/child_process.html” rel =“ nofollow noreferrer”>子进程重新启动它。

为了在杀死该过程之前找到该过程的PID,我使用了以下方式:

const childProcess = require('child_process');

function printPid() {
    childProcess.exec('pidof -s myProcess', function(error, stdout, stderr) {
        if (error) {
            console.log(error);
        } else {
            console.log(stdout);
        }
    });
}

I ended up using process.kill to kill the process and nodejs child process to restart it.

To find the pid of the process before killing it, I used this:

const childProcess = require('child_process');

function printPid() {
    childProcess.exec('pidof -s myProcess', function(error, stdout, stderr) {
        if (error) {
            console.log(error);
        } else {
            console.log(stdout);
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文