简体   繁体   中英

How to run node js using forever without stopping?

I am running a chat server in my application using node js. I have installed forever in the server. Usually, I start node using forever by the following command:

forever start server.js &

The problem is that the javascript file stops running after some time. It shows the following in the terminal:

Write failed: Broken pipe

When I login to my server once again and type the start command, the node keeps up. What command should I use to keep forever running even after my local session gets logged out?

To run a script infinitely as a background task, you need process manager tools. PM2 is my favorite process manager tool made in nodejs but can run any terminal task because it is a CLI.

  1. Basically, you can install NodeJs and npm to reach pm2. (You can visit NodeJs.org to download the installer.)

  2. You need to install the pm2 as a global module using npm install -g pm2 on your terminal

  3. You can check if it is installed simply by pm2 -v

  4. Then you can start your nodejs script on your terminal using pm2 start file_name.js

  5. It will create a thread in background to run your script and it will be restart forever.

  6. If you were doing something that takes so much time and you dont want to see the task running on the terminal you can just disable restarting by adding the parameter --no-autorestart into the command. (# pm2 start file_name.js --no-autorestart )

  7. If you want to see the logs or the state of the task, you can just use pm2 status , pm2 logs and pm2 monit .

  8. If you want to stop the task, you can use pm2 stop task_name

  9. You can use pm2 reload all or pm2 update to start all the tasks back

  10. You can kill the task using pm2 kill

For more information you can visit PM2 Documentation

Assuming you're on some Linux system, you could use screen . It's incredibly simple.

There are many tutorials out there, just google for 'screen linux'

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