gpt4 book ai didi

javascript - 机器重启后 pm2 托管进程处于错误状态

转载 作者:行者123 更新时间:2023-12-02 23:03:55 30 4
gpt4 key购买 nike

我正在使用pm2 (版本3.4.1)管理Laravel Echo websocket server 。它运行完美,但我试图让它在机器重新启动(运行 Ubuntu 16.04)后自动启动。为此,我遵循 these instructions :

$ pm2 startup
[PM2] Init System found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/local/share/.config/yarn/global/node_modules/pm2/bin/pm2 startup systemd -u kramer65 --hp /home/kramer65

$ sudo env PATH=$PATH:/usr/bin /usr/local/share/.config/yarn/global/node_modules/pm2/bin/pm2 startup systemd -u kramer65 --hp /home/kramer65
[sudo] password for kramer65:
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=kramer65
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/home/kramer65/.pm2
PIDFile=/home/kramer65/.pm2/pm2.pid

ExecStart=/usr/local/share/.config/yarn/global/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/share/.config/yarn/global/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/share/.config/yarn/global/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-kramer65.service
Command list
[ 'systemctl enable pm2-kramer65' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-kramer65.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-kramer65...
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via: $ pm2 save

[PM2] Remove init script via: $ pm2 unstartup systemd

然后我运行了pm2 save:

$ pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /home/kramer65/.pm2/dump.pm2

此后,我重新启动计算机,重新登录 ssh,并检查 pm2 进程:

$ pm2 status
┌──────────┬────┬─────────┬──────┬─────┬─────────┬─────────┬────────┬─────┬────────┬────────┬──────────┐
│ App name │ id │ version │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼─────────┼──────┼─────┼─────────┼─────────┼────────┼─────┼────────┼────────┼──────────┤
│ echo │ 0 │ N/A │ fork │ N/A │ errored │ 0 │ 0 │ 0% │ 0 B │ kramer65 │ disabled │
└──────────┴────┴─────────┴──────┴─────┴─────────┴─────────┴────────┴─────┴────────┴────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app

如您所见,状态错误。如果我停止并启动该进程(pm2 stop echo && pm2 start echo),该进程将再次运行。

错误日志(~/.pm2/pm2.log)告诉我:

2019-05-14T11:56:36: PM2 log: ===============================================================================
2019-05-14T11:56:36: PM2 log: --- New PM2 Daemon started ----------------------------------------------------
2019-05-14T11:56:36: PM2 log: Time : Tue May 14 2019 11:56:36 GMT+0200 (CEST)
2019-05-14T11:56:36: PM2 log: PM2 version : 3.4.1
2019-05-14T11:56:36: PM2 log: Node.js version : 8.15.1
2019-05-14T11:56:36: PM2 log: Current arch : x64
2019-05-14T11:56:36: PM2 log: PM2 home : /home/kramer65/.pm2
2019-05-14T11:56:36: PM2 log: PM2 PID file : /home/kramer65/.pm2/pm2.pid
2019-05-14T11:56:36: PM2 log: RPC socket file : /home/kramer65/.pm2/rpc.sock
2019-05-14T11:56:36: PM2 log: BUS socket file : /home/kramer65/.pm2/pub.sock
2019-05-14T11:56:36: PM2 log: Application log path : /home/kramer65/.pm2/logs
2019-05-14T11:56:36: PM2 log: Process dump file : /home/kramer65/.pm2/dump.pm2
2019-05-14T11:56:36: PM2 log: Concurrent actions : 2
2019-05-14T11:56:36: PM2 log: SIGTERM timeout : 1600
2019-05-14T11:56:36: PM2 log: ===============================================================================
2019-05-14T11:56:36: PM2 log: App [echo:0] starting in -fork mode-
2019-05-14T11:56:36: PM2 log: App [echo:0] online
2019-05-14T11:56:36: PM2 error: Error: spawn node ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
at onErrorNT (internal/child_process.js:362:16)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)

有人知道这里出了什么问题吗?

最佳答案

ENOENT代表No such directory entry

什么是env <command>正在做? (by Chris Johnsen)

env command用于在自定义环境中运行另一个程序而不修改当前程序。

sudo env PATH=$PATH:/usr/bin \
/usr/local/share/.config/yarn/global/node_modules/pm2/bin/pm2 \
startup systemd -u kramer65 --hp /home/kramer65

Installation note from pm2 documentation To detect available init systems on your machine and generate a configuration, use:

pm2 startup
$ [PM2] You have to run this command as root. Execute the following command:
$ sudo su -c env PATH=$PATH:/home/unitech/.nvm/versions/node/v4.3/bin pm2 startup <distribution> -u <user> --hp <home-path>

我将把命令分解如下

sudo env name=value name2=value2 program and args

1) 计划

运行命令程序 /usr/local/share/.config/yarn/global/node_modules/pm2/bin/pm2

The first remaining argument specifies the program name to invoke; it is searched for according to the `PATH' environment variable.

2)论据通过

Any remaining arguments are passed as arguments to that program.

args传递至pm2 cli 命令是 startup systemd -u kramer65 --hp /home/kramer65 .

  • startups命令将简单地设置 pm2在启动时启动 ( Startup Cli Commands )
  • systemd命令用于支持的 init 系统 Ubuntu => 16, CenOS >=7, Arch, Debian >=7 ( Startup Hooks Compatability ) 并应更改为正确的 platform正如所包含的文档所示。

3) 用于执行程序的环境

with an environment formed by extending the current environment with the environment variables and values designated by name=value and name2=value2.

pm2 docs 应该是 /home/kramer/.nvm/versions/node/v4.3/bin 内的文件夹

PATH=$PATH:/home/unitech/.nvm/versions/node/v4.3/bin

当您使用 home 之外的文件夹时目录,因为您在 nodejs 上运行旧版本(参见上一章 Nodejs 安装在 usr/bin/node 中)。

PATH=$PATH:/usr/bin

4) 用户权限

您正在通过--user kramer65 --home-path /home/kramer65pm2 cli 命令。

-u <user> --hp <home-path>

保存您的流程

The startup hook auto load the process list that you have previously saved. Save your process list with:

pm2 save

Nodejs安装在usr/bin/node (Error: spawn ENOENT on Ubuntu 14.04)

How to reproduce: you have two version of nodejs on your computer. One is an old version installed in /usr/bin/node and the other one is a recent version located in ./nodejs/node. I launch my pm2 manager programmatically, by calling ./nodejs/node myManager.js, which will call pm2.connect(), which will try to spawn the PM2 Daemon.

What happen is that the PM2 Daemon will be launched using the old node binary from /usr/bin/node instead of the recent node binary running my myManager.js script (locatedin ./nodejs/node).

What this does is look for a node binary in the current directory, and then default to node binaries in the path. BUT the node binary running the current code is in ./nodejs/node. To launch the PM2 daemon with the same node binary as the current process, you need to use process.execPath

您可以阅读more info at in the relevant discussion

关于javascript - 机器重启后 pm2 托管进程处于错误状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56131140/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com