简体   繁体   English

AWS EC2 AMI 用户数据运行 PM2

[英]AWS EC2 AMI User Data run PM2

I want to start my application on an instance boot.我想在实例启动时启动我的应用程序。 I used the following user data, but I'm unable to see any process running in pm2.我使用了以下用户数据,但我看不到任何在 pm2 中运行的进程。

Note: Creating an instance using an AMI.注意:使用 AMI 创建实例。 NodeJs, PM2 and all the dependencies areinstalled. NodeJs、PM2 和所有依赖项都已安装。 I can SSH to server and start my application using pm2 manually.我可以 SSH 到服务器并手动使用 pm2 启动我的应用程序。

#!/bin/bash
pm2 start /home/ubuntu/foyrinfrasubscription/server/server.js --name "ReportSub"
pm2 save
pm2 resurrect

I recommend using pm2 startup command to create init services script我推荐使用 pm2 启动命令来创建初始化服务脚本

Manual:手动的:

https://pm2.keymetrics.io/docs/usage/startup/ https://pm2.keymetrics.io/docs/usage/startup/

Example:例子:

https://futurestud.io/tutorials/pm2-restart-processes-after-system-reboot https://futurestud.io/tutorials/pm2-restart-processes-after-system-reboot

I had some issues with this because I wanted to set environment variables using user data and the regular pm2 startup script by default also runs at the same time as the user data (cloud-final) script is run.我对此有一些问题,因为我想使用用户数据设置环境变量,默认情况下,常规的pm2 startup脚本也与用户数据(cloud-final)脚本同时运行。

I found a really hacky way to do this - I wouldn't advise it:我找到了一种非常老套的方法 - 我不建议这样做:

First I had to change the order that pm2 starts up to make sure it fires AFTER the user data settings reference: https://serverfault.com/questions/871328/start-service-after-aws-user-data-has-run首先,我必须更改 pm2 启动的顺序,以确保它在用户数据设置参考后触发: https://serverfault.com/questions/871328/start-service-after-aws-user-data-has-run

Then, I couldn't find a way to set environment variables because they were in a different session.然后,我找不到设置环境变量的方法,因为它们位于不同的 session 中。 Dumping to .bashrc and /etc/profiles didn't work.转储到.bashrc/etc/profiles不起作用。 So, I just modified .pm2/dump.pm2 with my settings.所以,我只是用我的设置修改.pm2/dump.pm2

If anyone has any advice about how to do this better, that would be great!如果有人对如何更好地做到这一点有任何建议,那就太好了!

In addition to setting up pm2 via systemd as described in the other answers, using a pm2 "ecosystem" file allows passing environment variables harvested in User Data.除了如其他答案中所述通过 systemd 设置 pm2 之外,使用 pm2“生态系统”文件还允许传递在用户数据中收集的环境变量。 As the setup would likely be oriented to running a specific node service vs. a generic pm2-managed server, the systemd service file can be updated to use the ecosystem file programmatically edited in User Data flow, eg,由于设置可能面向运行特定节点服务与通用 pm2 管理的服务器,因此可以更新 systemd 服务文件以使用在用户数据流中以编程方式编辑的生态系统文件,例如,

ExecStart=/usr/lib/node_modules/pm2/bin/pm2 start /etc/systemd/system/pm2-ecosystem-my.config.js ExecStart=/usr/lib/node_modules/pm2/bin/pm2 start /etc/systemd/system/pm2-ecosystem-my.config.js

and the ExecReload line commented out (optionally)并且 ExecReload 行被注释掉(可选)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM