首页 > 解决方案 > NODE APP:Systemd 启动脚本不起作用?

问题描述

尝试为在端口 3000 上运行的 nodejs 应用程序创建启动脚本。

问题:node-app.server 脚本不起作用,我认为这是因为ExecStart路径错误。当我在 Chrome 中访问服务器 IP 时,什么都没有显示。

节点应用程序是使用 npm 生成器创建的,我通常使用它npm start来启动应用程序。我在这里添加了 bin/wwww 的路径:

[Unit]
Description=tweetMonster twtiter server - making your environment variables rad
Documentation=https://example.com
After=network.target

[Service]
Environment=NODE_PORT=3000
Type=simple
User=ubuntu
ExecStart=/home/ubuntu/twitter-server/bin/www.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

我的应用程序在 Ubuntu 18 上运行/home/ubuntu/twitter-server。如果这样做ls

/twitter-server$ ls
app.js  node_modules       package.json  routes
bin     package-lock.json  public        views

请帮忙!

错误终端:

Nov 01 05:40:25 ip-172-31-22-207 systemd[1]: node-app.service: Main process exited, code=exited, status=203/EXEC
Nov 01 05:40:25 ip-172-31-22-207 systemd[1]: node-app.service: Failed with result 'exit-code'.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: node-app.service: Service hold-off time over, scheduling restart.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: node-app.service: Scheduled restart job, restart counter is at 5.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: Stopped hello_env.js - making your environment variables rad.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: node-app.service: Start request repeated too quickly.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: node-app.service: Failed with result 'exit-code'.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: Failed to start hello_env.js - making your environment variables rad.
Nov 01 06:20:11 ip-172-31-22-207 systemd[1]: /etc/systemd/system/node-app.service:10: Executable path is not absolute: "node /home/ubuntu/twitter-server/bin/www.js"
Nov 01 06:24:35 ip-172-31-22-207 systemd[1]: /etc/systemd/system/node-app.service:10: Executable path is not absolute: "node /home/ubuntu/twitter-server/bin/www.js"
root@ip-172-31-22-207:/etc/systemd/system# 

标签: node.jssystemdupstartubuntu-18.04

解决方案


您的服务配置有 2 个问题。

首先,Environment用双引号将 的值括起来:

Environment="NODE_PORT=3000"

第二,

您需要使用 node来运行ExecStart脚本。/home/ubuntu/twitter-server/bin/www.js本身不是命令。

做,

ExecStart=/bin/bash -c '$$(which node) /home/ubuntu/twitter-server/bin/www.js'

推荐阅读