How do I daemonize a node.js script?

Asked 2 years ago, Updated 2 years ago, 64 views

If you want to daemonize a script created with node.js.
For example, if you want a program like screenshot-as-a-service to reside on the server.

node.js

2022-09-30 11:21

3 Answers

Let's use forever.
Monitor the life and death of the node.js script that you started, and restart it automatically if it stops.

npm install forever-g
forever start spp.js

Stop or restart a running script

forever stop
forever stopall
forever restart


2022-09-30 11:21

How about creating a unit file for systemd (although I'm not familiar with node.js)?


2022-09-30 11:21

I think it's better to manage it using pm2.The reason is that Node.js supports clustering mode or not at the language level, so I think it would be appropriate to use pm2 as a framework to absorb it.

I don't think the pm2 process itself is likely to die if left alone, but if we do our best, I think it would be better to daemonize pm2 itself at the OS level with systemd, as Hayao Yamada said.


2022-09-30 11:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.