我建立了一个Nodejs项目,现在运行顺畅。
我使用forever
服务来在后台运行文件,但如果服务器重新启动,
守护进程不会自动启动,需要手动启动。
我想要即使服务器重新启动也能运行守护进程。
您可以在.bash_profile
中添加forever命令,这样每次服务器重新启动时,您的命令也将被执行。
nano ~/.bash_profile
forever start app.js # add this command to the file, or whatever command you are using.
source ~/.bash_profile # very important, else changes will not take effect
touch /etc/systemd/system/[your-app-name].service
nano /etc/systemd/system/[your-app-name].service
[Unit]
Description=Node.js Example Server
#Requires=After=mysql.service # Requires the mysql service to run first
[Service]
ExecStart=/usr/local/bin/node /opt/nodeserver/server.js
# Required on some systems
# WorkingDirectory=/opt/nodeserver
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-example
#User=<alternate user>
#Group=<alternate group>
Environment=NODE_ENV=production PORT=1337
[Install]
WantedBy=multi-user.target
启用该服务,将会标记该服务在启动时自动启动。
systemctl enable [your-app-name].service
管理服务
systemctl start [your-app-name].service
systemctl stop [your-app-name].service
systemctl status [your-app-name].service # ensure your app is running
systemctl restart [your-app-name].service
参考资料: https://www.axllent.org/docs/view/nodejs-service-with-systemd/
感谢@dlmeetei分享链接。
bash
时启动服务器。最好与systemd
集成或基于lsb编写init.d脚本。 - dlmeetei
systemd
集成,它有许多优点。 - dlmeetei