永久使用时如何将节点应用程序作为系统服务启动?
How to start node app as systemd service when it uses forever?
我是 node 的新手,正在学习 Ryan Lewis 的优秀 AWS 开发人员教程。在该课程中,我们学习使用来自 AWS Marketplace 的 Bitnami Node.js 图像将 Node.js 应用程序部署到 AWS EC2。为了练习一下,我想使用 systemd
将应用程序变成一项服务,以便它在重启后恢复。然而,经过大量调试后,我发现该服务似乎一直在重新启动,并且该应用程序从未上线。这可能是由应用程序的启动方式引起的。它是 运行 使用 forever
CLI 工具。当我手动 运行 npm start
时,我看到以下输出:
npm start
> hbfl@1.0.0 prestart /home/bitnami/hamstercourse
> npm run build
> hbfl@1.0.0 build /home/bitnami/hamstercourse
> webpack
(node:19917) DeprecationWarning: Chunk.modules is deprecated. Use Chunk.getNumberOfModules/mapModules/forEachModule/containsModule instead.
Hash: aa4bec1a367d114f2c7f
Version: webpack 3.3.0
Time: 12349ms
Asset Size Chunks Chunk Names
application.min.js 363 kB 0 [emitted] [big] application
stylesheet.css 13.4 kB 0 [emitted] application
[10] ./node_modules/react-redux/es/index.js + 14 modules 37.6 kB {0} [built]
[18] ./node_modules/react-router-dom/es/index.js + 13 modules 11.9 kB {0} [built]
[59] ./node_modules/redux/es/index.js + 6 modules 21.3 kB {0} [built]
[62] ./node_modules/react-router-redux/es/index.js + 4 modules 5.87 kB {0} [built]
[105] ./app/index.jsx 1.86 kB {0} [built]
[209] ./app/router.jsx 2.85 kB {0} [built]
[211] ./app/routes/index.jsx 1.65 kB {0} [built]
[287] ./app/reducers/index.js 316 bytes {0} [built]
[288] ./app/reducers/hamsters.js 717 bytes {0} [built]
[289] ./app/reducers/races.js 649 bytes {0} [built]
[290] ./app/reducers/user.js 2.32 kB {0} [built]
[291] ./app/reducers/leaderboards.js 355 bytes {0} [built]
[292] ./app/reducers/status.js 285 bytes {0} [built]
[293] ./app/index.less 41 bytes {0} [built]
[326] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/index.less 247 bytes [built]
+ 312 hidden modules
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/index.less 247 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Config/index.less 485 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/User/index.less 275 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Leaderboards/index.less 485 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Races/index.less 485 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Race/index.less 485 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Main/index.less 501 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Login/index.less 488 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Hamster/index.less 485 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Hamsters/index.less 617 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Main/Hero/index.less 827 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
> hbfl@1.0.0 start /home/bitnami/hamstercourse
> forever stopall && ./node_modules/.bin/forever start index.js
info: No forever processes running
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: index.js
然后该进程进入后台,可以使用 forever list
、forever stop
等进行管理。如 here 所述,systemd
会终止后台进程(并使用这背后的理由很充分)。
为了证实我的怀疑,我尝试 运行 像这样连接服务:
[Unit]
Description=Node.js Hamster Http Server
[Service]
PIDFile=~/hamster-99.pid
User=bitnami
Group=bitnami
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
KillSignal=SIGQUIT
WorkingDirectory=/home/bitnami/hamstercourse
ExecStart=/opt/bitnami/nodejs/bin/node /home/bitnami/hamstercourse/index.js
[Install]
WantedBy=multi-user.target
启用服务并重新加载守护程序后,输出为:
hamster.service - Node.js Hamster Http Server
Loaded: loaded (/etc/systemd/system/hamster.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-06-26 10:57:46 UTC; 28min ago
Main PID: 19847 (.node.bin)
Tasks: 11
Memory: 26.8M
CPU: 2.019s
CGroup: /system.slice/hamster.service
+-19847 /opt/bitnami/nodejs/bin/.node.bin /home/bitnami/hamstercourse/index.js
Jun 26 10:57:46 ip-172-31-35-115 systemd[1]: hamster.service: Main process exited, code=dumped, status=3/QUIT
Jun 26 10:57:46 ip-172-31-35-115 systemd[1]: Stopped Node.js Hamster Http Server.
Jun 26 10:57:46 ip-172-31-35-115 systemd[1]: hamster.service: Unit entered failed state.
Jun 26 10:57:46 ip-172-31-35-115 systemd[1]: hamster.service: Failed with result 'core-dump'.
Jun 26 10:57:46 ip-172-31-35-115 systemd[1]: Started Node.js Hamster Http Server.
Jun 26 10:57:48 ip-172-31-35-115 node[19847]: Server started at http://localhost:3000
好消息是:这实际上是 运行 的服务。欢呼!但是,它跳过了很多 npm start
实际上 运行 的基本步骤(比如缩小应用程序),当然它不会 运行 通过 forever
应用程序。人们可能会讨论在 运行 宁作为一项服务时是否需要使用像 forever
这样的管理工具,但是应该可以从 systemd
运行 它而不改变应用程序,对吧?那我该怎么做呢?
更新:
我刚找到 https://unix.stackexchange.com/questions/308311/systemd-service-runs-without-exiting 并尝试在单元文件中使用 Type=forking
。这实际上似乎有效。但这是方法吗?或者还有其他最佳实践吗?
我认为Type=forking
是正确的。
https://www.freedesktop.org/software/systemd/man/systemd.service.html
If set to forking, it is expected that the process configured with
ExecStart= will call fork() as part of its start-up. The parent
process is expected to exit when start-up is complete and all
communication channels are set up. The child continues to run as the
main service process, and the service manager will consider the unit
started when the parent process exits. This is the behavior of
traditional UNIX services. If this setting is used, it is recommended
to also use the PIDFile= option, so that systemd can reliably identify
the main process of the service. systemd will proceed with starting
follow-up units as soon as the parent process exits.
这就是 forever
的工作原理。还要注意 apache2
服务也使用 Type=forking
.
我是 node 的新手,正在学习 Ryan Lewis 的优秀 AWS 开发人员教程。在该课程中,我们学习使用来自 AWS Marketplace 的 Bitnami Node.js 图像将 Node.js 应用程序部署到 AWS EC2。为了练习一下,我想使用 systemd
将应用程序变成一项服务,以便它在重启后恢复。然而,经过大量调试后,我发现该服务似乎一直在重新启动,并且该应用程序从未上线。这可能是由应用程序的启动方式引起的。它是 运行 使用 forever
CLI 工具。当我手动 运行 npm start
时,我看到以下输出:
npm start
> hbfl@1.0.0 prestart /home/bitnami/hamstercourse
> npm run build
> hbfl@1.0.0 build /home/bitnami/hamstercourse
> webpack
(node:19917) DeprecationWarning: Chunk.modules is deprecated. Use Chunk.getNumberOfModules/mapModules/forEachModule/containsModule instead.
Hash: aa4bec1a367d114f2c7f
Version: webpack 3.3.0
Time: 12349ms
Asset Size Chunks Chunk Names
application.min.js 363 kB 0 [emitted] [big] application
stylesheet.css 13.4 kB 0 [emitted] application
[10] ./node_modules/react-redux/es/index.js + 14 modules 37.6 kB {0} [built]
[18] ./node_modules/react-router-dom/es/index.js + 13 modules 11.9 kB {0} [built]
[59] ./node_modules/redux/es/index.js + 6 modules 21.3 kB {0} [built]
[62] ./node_modules/react-router-redux/es/index.js + 4 modules 5.87 kB {0} [built]
[105] ./app/index.jsx 1.86 kB {0} [built]
[209] ./app/router.jsx 2.85 kB {0} [built]
[211] ./app/routes/index.jsx 1.65 kB {0} [built]
[287] ./app/reducers/index.js 316 bytes {0} [built]
[288] ./app/reducers/hamsters.js 717 bytes {0} [built]
[289] ./app/reducers/races.js 649 bytes {0} [built]
[290] ./app/reducers/user.js 2.32 kB {0} [built]
[291] ./app/reducers/leaderboards.js 355 bytes {0} [built]
[292] ./app/reducers/status.js 285 bytes {0} [built]
[293] ./app/index.less 41 bytes {0} [built]
[326] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/index.less 247 bytes [built]
+ 312 hidden modules
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/index.less 247 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Config/index.less 485 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/User/index.less 275 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Leaderboards/index.less 485 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Races/index.less 485 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Race/index.less 485 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Main/index.less 501 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Login/index.less 488 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Hamster/index.less 485 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Hamsters/index.less 617 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
[0] ./node_modules/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./node_modules/less-loader/dist!./app/scenes/Main/Hero/index.less 827 bytes {0} [built]
+ 1 hidden module
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
Child extract-text-webpack-plugin:
2 modules
> hbfl@1.0.0 start /home/bitnami/hamstercourse
> forever stopall && ./node_modules/.bin/forever start index.js
info: No forever processes running
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: index.js
然后该进程进入后台,可以使用 forever list
、forever stop
等进行管理。如 here 所述,systemd
会终止后台进程(并使用这背后的理由很充分)。
为了证实我的怀疑,我尝试 运行 像这样连接服务:
[Unit]
Description=Node.js Hamster Http Server
[Service]
PIDFile=~/hamster-99.pid
User=bitnami
Group=bitnami
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
KillSignal=SIGQUIT
WorkingDirectory=/home/bitnami/hamstercourse
ExecStart=/opt/bitnami/nodejs/bin/node /home/bitnami/hamstercourse/index.js
[Install]
WantedBy=multi-user.target
启用服务并重新加载守护程序后,输出为:
hamster.service - Node.js Hamster Http Server
Loaded: loaded (/etc/systemd/system/hamster.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-06-26 10:57:46 UTC; 28min ago
Main PID: 19847 (.node.bin)
Tasks: 11
Memory: 26.8M
CPU: 2.019s
CGroup: /system.slice/hamster.service
+-19847 /opt/bitnami/nodejs/bin/.node.bin /home/bitnami/hamstercourse/index.js
Jun 26 10:57:46 ip-172-31-35-115 systemd[1]: hamster.service: Main process exited, code=dumped, status=3/QUIT
Jun 26 10:57:46 ip-172-31-35-115 systemd[1]: Stopped Node.js Hamster Http Server.
Jun 26 10:57:46 ip-172-31-35-115 systemd[1]: hamster.service: Unit entered failed state.
Jun 26 10:57:46 ip-172-31-35-115 systemd[1]: hamster.service: Failed with result 'core-dump'.
Jun 26 10:57:46 ip-172-31-35-115 systemd[1]: Started Node.js Hamster Http Server.
Jun 26 10:57:48 ip-172-31-35-115 node[19847]: Server started at http://localhost:3000
好消息是:这实际上是 运行 的服务。欢呼!但是,它跳过了很多 npm start
实际上 运行 的基本步骤(比如缩小应用程序),当然它不会 运行 通过 forever
应用程序。人们可能会讨论在 运行 宁作为一项服务时是否需要使用像 forever
这样的管理工具,但是应该可以从 systemd
运行 它而不改变应用程序,对吧?那我该怎么做呢?
更新:
我刚找到 https://unix.stackexchange.com/questions/308311/systemd-service-runs-without-exiting 并尝试在单元文件中使用 Type=forking
。这实际上似乎有效。但这是方法吗?或者还有其他最佳实践吗?
我认为Type=forking
是正确的。
https://www.freedesktop.org/software/systemd/man/systemd.service.html
If set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up. The parent process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the main service process, and the service manager will consider the unit started when the parent process exits. This is the behavior of traditional UNIX services. If this setting is used, it is recommended to also use the PIDFile= option, so that systemd can reliably identify the main process of the service. systemd will proceed with starting follow-up units as soon as the parent process exits.
这就是 forever
的工作原理。还要注意 apache2
服务也使用 Type=forking
.