简体   繁体   English

NodeJS服务器如何运行

[英]How NodeJS server running for ever

I use Putty. 我用Putty。 When I start server with "node X.js", putty start server running. 当我使用“node X.js”启动服务器时,putty启动服务器正在运行。 If I exit from putty the server stop. 如果我从putty退出服务器停止。 How I can keep it running and make it default running after restart or reboot the server (computer)? 如何在重启或重启服务器(计算机)后保持运行并使其默认运行?

I have centos 5.10. 我有1.50。 Thank you! 谢谢!

I use pm2 to do it 我用pm2来做

To install pm2 安装pm2

sudo npm install -g pm2

To generate startup script 生成启动脚本

pm2 startup ubuntu(centos in your case)

Then pm2 will prompt the command for you to run, in my case, it is like 然后pm2会提示你运行命令,在我的情况下,它就像

PM2 You have to run this command as root
PM2 Execute the following command :
PM2 sudo env PATH=$PATH:/usr/bin pm2 startup ubuntu -u USERNAME

Then you could run 然后你就可以跑了

sudo env PATH=$PATH:/usr/bin pm2 startup ubuntu -u USERNAME

Then you could see 然后你可以看到

PM2 Generating system init script in /etc/init.d/pm2-init.sh
PM2 Making script booting at startup...
PM2 -ubuntu- Using the command su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"
Adding system startup for /etc/init.d/pm2-init.sh ...
   /etc/rc0.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc1.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc6.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc2.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc3.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc4.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc5.d/S20pm2-init.sh -> ../init.d/pm2-init.sh

PM2 Done.

Once you have started the apps and want to keep them on server reboot do: 启动应用程序并希望将它们保留在服务器重启后,请执行以下操作:

 pm2 save

You can refer to Startup script section in https://github.com/Unitech/pm2#startup-script 您可以参考https://github.com/Unitech/pm2#startup-script中的“启动脚本”部分

There are several ways, I personally like forever. 有几种方式,我个人喜欢永远。

sudo npm install -g forever
forever start app.js &

note that ending with & will fork the process to background. 请注意,以&结尾将进程转换为后台。

You can later check the process with 您可以稍后检查该过程

forever list

To run it when your system restarts you can add to cron 要在系统重新启动时运行它,您可以添加到cron

@reboot forever start app.js  &> /dev/null

Remember to point to the absolute location of app.js 请记住指向app.js的绝对位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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