解决方法
我使用pm2来做到这一点
要安装pm2
sudo npm install -g pm2
生成启动脚本
pm2 startup ubuntu(centos in your case)
那么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
然后你可以跑
sudo env PATH=$PATH:/usr/bin pm2 startup ubuntu -u USERNAME
然后你可以看到
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.
一旦您启动了应用程序,并希望将它们保留在服务器重新启动上:
pm2 save
您可以参考https://github.com/Unitech/pm2#startup-script中的启动脚本部分