自动启动 NodeJS 应用程序的 Systemd 单元 (npm start)
此 systemd 单元文件使用 npm start 自动启动你的 NodeJS 服务。将其放在 /etc/systemd/system/myapplication.service 中(将 myapplication 替换为你的应用程序名称!)
myapplication.service
[Unit]
Description=我的应用程序
[Service]
Type=simple
Restart=always
User=nobody
Group=nobody
WorkingDirectory=/opt/myapplication
ExecStart=/usr/bin/npm start
[Install]
WantedBy=multi-user.target替换:
- 将
/opt/myapplication替换为你的应用程序目录(package.json所在位置) - 将
User=nobody和Group=nobody替换为你想要运行服务的用户和组 - Optionally you can add a custom decription instead of
Description=我的应用程序
然后启用开机启动并立即启动:(记住将 myapplication 替换为你选择的服务文件名称!)
enable_myapp.sh
sudo systemctl enable --now myapplication启动使用
start_myapp.sh
sudo systemctl start myapplication重启使用
restart_myapp.sh
sudo systemctl restart myapplication停止使用
stop_myapp.sh
sudo systemctl stop myapplication查看并跟踪日志:
journalctl_follow.sh
sudo journalctl -xfu myapplication在 less 中查看日志:
journalctl_less.sh
sudo journalctl -xu myapplicationCheck out similar posts by category:
Linux
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow