简体   繁体   English

使用环境变量定义PM2脚本路径

[英]Define PM2 script path with environment variable

I'm building a simple staging CI environment where I define the path of the runnable Node application by an environment variable APP_PATH. 我正在构建一个简单的暂存CI环境,其中我通过环境变量APP_PATH定义了可运行Node应用程序的路径。

Is it possible to tell PM2 in a process config file to look up the startup script by following the path in the APP_PATH variable? 是否可以通过遵循APP_PATH变量中的路径告诉进程配置文件中的PM2查找启动脚本?

Like this: 像这样:

{
  "apps": [
    {
      "name": "my_app",
      "script": "$APP_PATH/app.js"
    }
  ]
}

Currently, I get a [PM2][ERROR] script not found : message from PM2 when starting the above configuration. 当前,启动上述配置时,从PM2收到[PM2][ERROR] script not found :消息。

In a nutshell: PM2 doesn't resolve the env var defined in the script property. 简而言之: PM2不会解析script属性中定义的env var。 Is there any way to overcome this? 有什么办法可以克服这个问题?

If you need to do in the same approach. 如果您需要采用相同的方法。 Follow this. 遵循这个。

Create a .json file outside your root directory or wherever you need it. 在根目录之外或需要的地方创建一个.json文件。

servers.json servers.json

[{
  "name":"MyApp",
  "script":"/home/user/app/server.js",
  "instances":"3" //number of instances to start
},
{
  "name":"MySecondApp",
  "script":"/home/user/app2/server.js",
  "instances":"max" //to calculate your number of CPU cores available and run based on the core count
}]

Then run using pm2 start servers.json 然后使用pm2 start servers.json运行pm2 start servers.json

This will start the two app with the name and instances mentioned. 这将以提到的名称和实例启动两个应用程序。

Or 要么

With the latest version of PM2 you do not need to write any scripts. 使用最新版本的PM2,您无需编写任何脚本。 Just execute some commands to do the same. 只需执行一些命令即可。

Step 1: First you create your pm2 instances as you need like now many instances you need to run or how many different server you need to run. 步骤1:首先,根据需要创建pm2实例,就像现在需要运行多个实例或需要运行多少台不同的服务器一样。

Mine is below 我的在下面 在此处输入图片说明

Once you apps are started and listed like this. 一旦启动应用程序并按如下所示列出。

Step 2: Type pm2 startup . 步骤2:输入pm2 startup Then you will see a auto generated command by pm2 which helps you to create it as a service. 然后,您将看到pm2自动生成的命令,可帮助您将其创建为服务。

在此处输入图片说明

Step 3: You will see the command you need to run in grey shade. 步骤3:您将看到需要以灰色阴影运行的命令。 Copy that and run it as root user. 复制该文件,然后以root用户身份运行它。

once you run that command you will see result like below. 一旦运行该命令,您将看到如下结果。

在此处输入图片说明

Step 4: The run pm2 save so the present pm2 list of process will be save for startup script. 步骤4: pm2 save运行pm2 save以便pm2 save当前的pm2进程列表作为启动脚本。

That's it.. 而已..

Test it by rebooting your server and check using pm2 ls or pm2 status . 通过重新引导服务器进行测试,并使用pm2 lspm2 status检查。

In case you want to update the pm2 process list again the use pm2 update this will take the current process list and update the startup script. 如果要再次更新pm2进程列表,请使用pm2 update来获取当前进程列表并更新启动脚本。

Hope this helps!!! 希望这可以帮助!!!

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

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