[英]How to run node js even on server restart
我構建了一個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
下次,在服務器重新啟動時,您的命令也將運行,從而創建節點腳本的守護程序。
注意:這可能不是最好的解決方案,而是我得到的解決方案。
正如@dlmeetei建議的那樣,您也可以像服務一樣啟動nodejs應用程序,以便我們可以使用linux服務提供的功能。
首先在/etc/systemd/system
創建一個文件,如:
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分享鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.