Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Nodejs background service starts

Shulou Source: shulou.com Published: 2022-06-01 07:30:02 10月03日 Update

A recent project WeChat Mini Programs, need to write a small back-end program to deal with chat records saved, the main function is to set up a group chat room, so use node to write a websocket service.

But after the terminal connects to the server, runs node server.js, and exits the terminal, the service stops running.

I thought node server.js & or nohup node server.js > / dev/null 2 > & 1 & could be solved easily, but it turns out that this is not the case at all.

Google did some information and learned that

Nodejs is generally executed as a user command, and when the user disconnects the customer, the application stops, which is very annoying. How to make the nodejs application as a service and execute in the background?

Finally, use the forever package to solve the problem as follows:

Sudo npm install-g forever-- registry= http://registry.cnpmjs.org

Forever start your script file (such as server.js)

Forever list to view all processes that forever is running

Forever stop uid stops running the process of the specified uid

It can do more things, such as recording output and error logs separately, such as being used as an api in js. It can also be installed normally through the following (mine is a ubutun system)

$sudo npm install forever-g # install $forever start app.js # start $forever stop app.js # close $forever start-l forever.log-o out.log-e err.log app.js # output log and error

Command syntax and using h ttps: / / gi thub.c om / nod e ji ts u / fore ver

Tags: Services runs commands logs users programs terminals processes errors output background annoyance things functions things clients files solutions more servers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info Shulou Information NVidia Microsoft