简体   繁体   中英

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! 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. 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 . how can I disable this CTRL-z and others at all ? or is there any other solution maybe in my code or even modifying the bsd?

I found my answer in freebsd forums , here i want to share with you for those with same purpose.

As mentioned in that forum, I needed to wrap my program inside a C program which can detect and ignore signals . 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). Some of them are (my favourite is 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 . 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.

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. I suggest you to start using them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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