简体   繁体   English

防止Node.js应用程序退出

[英]Prevent a nodejs application from exiting

I've got a cli application written in nodejs using vorpal , live in a freebsd (v9.3), I need to know is there any way to prevent the user from exiting this app or not! 我有一个使用vorpal用nodejs编写的cli应用程序,它生活在freebsd(v9.3)中,我需要知道有什么方法可以防止用户退出该应用程序! I want it like when the application has started , it will never exit until reboot or shutting down the system. 我希望它像启动应用程序一样,直到重新启动或关闭系统后,它才会退出。 this is very critical and I mean it no way to exit the application at any cost . 这是非常关键的,我的意思是没有办法不惜一切代价退出该应用程序。 Is it possible at all ? 有可能吗?

Edit: This is what i want: when my program start ,user can not exit the program except my own exit command, So i want to somehow prevent CTRL-Z,CTRL-C Or any other things like them. 编辑:这就是我想要的:当我的程序启动时,用户只能退出自己的退出命令才能退出程序,所以我想以某种方式阻止CTRL-Z,CTRL-C或其他类似的东西。 I can handle the SIGINT and errors but my problem is with "CTRL-Z" which fires a SIGSTOP signal and node cant listen to it . 我可以处理SIGINT和错误,但是我的问题是“ CTRL-Z”触发了SIGSTOP信号,节点无法监听它。 how can I disable this CTRL-z and others at all ? 如何完全禁用此CTRL-z和其他按钮? or is there any other solution maybe in my code or even modifying the bsd? 还是在我的代码中甚至还有修改bsd的方法,还有其他解决方案吗?

I found my answer in freebsd forums , here i want to share with you for those with same purpose. 我在freebsd论坛中找到了答案,在这里我想与您分享相同目的的人。

As mentioned in that forum, I needed to wrap my program inside a C program which can detect and ignore signals . 如该论坛中所述,我需要将程序包装在可以检测和忽略信号的C程序中。 So this is the way: 所以这是这样的:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
        /* Ignore signals from keyboard */
        signal(SIGINT, SIG_IGN);
        signal(SIGTSTP, SIG_IGN);
        signal(SIGQUIT, SIG_IGN);

    system("node path/to/your/program");
    /* Whatever needs to be done if this point is reached. */
    return (0);
}

There is a few ways to accomplish this, depends on what you're asking: 有几种方法可以完成此操作,具体取决于您的要求:

1) using an external tool to restart the process on exit or crash (like Khauri McClain said in a comment). 1)使用外部工具在退出或崩溃时重新启动过程(例如Khauri McClain在评论中说)。 Some of them are (my favourite is pm2): 其中一些是(我最喜欢的是pm2):

2) You can prevent Node.js from exiting on error: Make node.js not exit on error , but please don't do it, this answer explains why: https://stackoverflow.com/a/13049037/1206421 . 2)您可以防止Node.js在错误时退出: 使node.js在错误时不退出 ,但是请不要这样做,此答案说明了原因: https : //stackoverflow.com/a/13049037/1206421 Also you can apparently ignore Control-C keypresses with this module: https://www.npmjs.com/package/ctrl-c (I didn't look into it), but this seems like a bad idea too. 同样,您显然可以使用以下模块忽略Control-C按键: https : //www.npmjs.com/package/ctrl-c (我没有研究过),但这似乎也不是一个好主意。

Anyway, if the process you are using will be killed by system or another user (for example, with kill -9 ), there's nothing you can do about it except for using some auto-restart services, like I described above. 无论如何,如果您正在使用的进程将被系统或其他用户kill -9 (例如,使用kill -9 ),则除了使用某些自动重启服务(如上所述)之外,您无能为力。 I suggest you to start using them. 我建议您开始使用它们。

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

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