Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nodejs background service starts

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report