In the simplest for using systemd service:
-
Install
forever:[sudo] npm install forever -g -
Write and store the script to run in preferred location.
-
Write the
Systemd service:[Unit] Description=forever service After=network.target [Service] ExecStart=/home/george/.npm-global/bin/forever start /root/node/node_modules/.bin/www ExecStop=/home/george/.npm-global/bin/forever stop /root/node/node_modules/.bin/www Restart=always RestartSec=10 # Restart service after 10 seconds if node service crashes StandardOutput=syslog # Output to syslog StandardError=syslog # Output to syslog SyslogIdentifier=nodejs-example [Install] WantedBy=multi-user.target -
Save the
systemd servicefile in/etc/systemd/systemasmyforever.service( or with whatever name you like ). -
Start the service and enable at start up.
sudo systemctl start myforever.service sudo systemctl enable myforever.service -
Check if it's running:
sudo systemctl status myforever.service -
To stop and disable it any time:
sudo systemctl stop myforever.service sudo systemctl disable myforever.service
NOTE:
- This is a simplified version of a
systemd servicemany options are available - The service can also be called
myforeverwithout the.serviceextension,systemdwill pick the right file - This
/home/george/.npm-global/bin/foreveris where mynodemodules are kept, yours will be different. Find it withwhich forever
Additional Information:
https://www.axllent.org/docs/view/nodejs-service-with-systemd/
ref link: https://askubuntu.com/questions/894665/add-startup-service-on-16-04
本文介绍了如何在Ubuntu 16.04上安装服务脚本,创建systemd服务文件,并设置开机启动。首先安装必要组件,然后将脚本保存到指定位置。接着创建并保存systemd服务文件,如`myservice.service`。通过命令启动服务并设置开机启动,可以使用`systemctl`检查服务状态。若要停止和禁用服务,也有相应命令。注意,service文件通常位于`/etc/systemd/system`目录下,可根据实际情况查找。参考链接提供了更多详细信息。
3122

被折叠的 条评论
为什么被折叠?



