pm2 无法在 Ubuntu 上启动

问题描述 投票:0回答:7

我在服务器重新启动时很难让 pm2 重新启动(本身和两个节点/express 文件,

app.js
app2.js
)。

以下是我尝试过的过程:

pm2 startup
pm2 start app.js
pm2 start app2.js
pm2 startup ubuntu (also tried systemd and with/without -u username)
pm2 save

我以各种可能的组合运行了上述命令,但没有任何效果。我尝试以 root 身份运行,但也不起作用。

我的

~/.pm2/dump.pm2
文件包含信息,所以我不知道还能在哪里查看。

我尝试根据

这个问题
修改我的/etc/init.d/pm2-init.sh文件,但没有帮助。

我的设置:
数字海洋服务器
Ubuntu 15.10
节点 v5.4.1
PM2 v 1.0.0

我尝试过的其他参考..
http://pm2.keymetrics.io/docs/usage/startup/
https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-product-environment-on-an-ubuntu-vps
https://gist.github.com/leommoore/5998406
https://www.terlici.com/2015/06/20/running-node-forever.html
https://serversforhackers.com/node-process-management-with-pm2 http://nodered.org/docs/getting-started/running.html#starting-node-red-on-boot
https://github.com/Unitech/pm2/issues/1316

到目前为止,每次我重新启动服务器时,pm2 都无法自动启动(除非我需要等待几分钟?- nginx 会立即重新启动)。

有人可以帮我解决这个问题吗?命令的运行顺序是什么?我需要修改任何其他文件吗?

ubuntu pm2
7个回答
25
投票

您必须使用

保存您的节点应用程序
 pm2 start [app_name]
 pm2 save

然后做:

 pm2 startup [operation system]

这将创建一个 dump.pm2 文件,pm2 需要该文件在重新启动时运行您的应用程序。

操作系统:

  • systemd:Ubuntu >= 16、CentOS >= 7、Arch、Debian >= 7
  • 暴发户:Ubuntu <= 14
  • launchd:达尔文,MacOSx
  • openrc:Gentoo Linux、Arch Linux
  • rcd:FreeBSD
  • 系统v:Centos 6、Amazon Linux

6
投票

我的解决方案是使用 systemctl 重新启动 pm2:

systemctl reload-or-restart pm2-root

当我第一次设置 Ubuntu 18.04 服务器时,我运行

pm2 start app.js
来启动我的应用程序。然后,当我尝试运行
pm2 startup
+
pm2 save
在启动时重新启动应用程序时,这似乎不起作用,因为通过运行
systemctl list-units
,pm2 没有显示在服务列表中。即使应用程序正在运行(
pm2 list
证实了这一点)。于是我跑了
systemctl list-units -all
,pm2显示为“inactive”和“dead”。

所以我做到了:

  • systemctl status pm2-root
    (只是为了确认它是“不活动”/“死亡”)

  • systemctl reload-or-restart pm2-root
    (通过systemctl重新启动pm2)

  • systemctl status pm2-root
    (确认 pm2 现在处于“活动”/“运行”状态)

  • 然后我还运行了

    systemctl enable pm2-root
    ,使PM2能够在启动时运行(不确定是否需要)

  • 然后再次

    pm2 startup
    +
    pm2 save
    (用于开机启动)

OBS.:我在命令中使用了

pm2-root
,因为我使用
root
用户运行 pm2,但如果需要,您应该替换为您的用户 (
pm2-<USER>
)。


4
投票

我想我解决了问题

我玩了pm2,发现了CentOS 7的两种工作方式(在你的 项目文件夹index.js - 主文件):

  • 1

    sudo pm2 启动index.js
    sudo pm2 保存
    sudo pm2 启动centos
    
  • 2

    pm2 启动index.js
    pm2保存
    pm2启动centos
    '# 并在 sudo 下运行前面代码中生成的脚本
    

1
投票

您是否尝试过检查 Ubuntu 上的启动日志(

cat /var/log/boot.log
)?

您可能会看到如下错误:

Error: EACCES, permission denied '/home/<USER>/.pm2'

我自己无法解决这个问题,但至少这应该为您指明正确的方向。


0
投票

在Ubuntu下得到如下代码:

pm2 start main.js
pm2 save
pm2 startup ubuntu

请注意,“pm2 save”将在 /root/.pm2/dump.pm2 中创建一个 dump.pm2 文件,并且该文件将在您的计算机重新启动时使用。

如果您不知道操作系统名称,请使用:

pm2 startup

PM2 将自动检测您的操作系统。


0
投票

解决方案是:u200e

  1. 移至安装 PM2 的文件夹(例如 /home/ubuntu)。 将会有u200e一个文件夹‘.pm2 u200e.u200e

  2. 运行‘PM2startup’来生成 活动启动脚本。u200e

  3. 运行“PM2 save”以冻结进程列表 自动重生。”。u200e

现在所有进程都会在roboot上自动启动。


0
投票

我知道它很旧,但这是我的版本:

  1. 使用 pm2 启动进程:

    pm2 start app_name.js

  2. 将进程保存在列表中:

    pm2 save

  3. 生成启动脚本:

    pm2 startup

在这里我们可以得到命令列表。

这些以绿色突出显示的命令之一必须在下一步中实现

  1. 激活脚本(上一步的命令):
    systemctl enable pm2-root

我发布此内容的原因是因为我认为最后一个命令是必需的并不明显。但如果没有它,重新启动时启动将无法工作。

© www.soinside.com 2019 - 2024. All rights reserved.