简体   繁体   English

pm2 可以运行“npm start”脚本吗

[英]Can pm2 run an 'npm start' script

Is there a way for pm2 to run an npm start script or do you just have to run pm2 start app.js有没有办法让 pm2 运行 npm start 脚本,或者你只需​​要运行pm2 start app.js

So in development所以在开发中

npm start

Then in production with pm2 you would run something like然后在使用 pm2 进行生产时,您将运行类似

pm2 start 'npm start'

There is an equivalent way to do this in forever :有一种等效的方法可以forever做到这一点:

forever start -c "npm start" ./

PM2 now supports npm start: PM2 现在支持 npm start:

pm2 start npm -- start

To assign a name to the PM2 process, use the --name option:要为 PM2 进程分配名称,请使用--name选项:

pm2 start npm --name "app name" -- start

Those who are using a configuration script like a .json file to run the pm2 process can use npm start or any other script like this -那些使用.json文件之类的配置脚本来运行 pm2 进程的人可以使用npm start或任何其他类似这样的脚本 -

my-app-pm2.json我的应用程序pm2.json

{
    "apps": [
        {
            "name": "my-app",
            "script": "npm",
            "args" : "start"
        }
    ]
}

Then simply -那么简单——

pm2 start my-app-pm2.json

Edit - To handle the use case when you have this configuration script in a parent directory and want to launch an app in the sub-directory then use the cwd attribute.编辑- 当您在父目录中有此配置脚本并希望在子目录中启动应用程序时,要处理用例,然后使用cwd属性。

Assuming our app is in the sub-directory nested-app relative to this configuration file then -假设我们的应用程序位于相对于这个配置文件的子目录nested-app ,那么 -

{
    "apps": [
        {
            "name": "my-nested-app",
            "cwd": "./nested-app",
            "script": "npm",
            "args": "start"
        }
    ]
}

More detail here .更多细节在这里

Yes.是的。 Use pm2 start npm --no-automation --name {app name} -- run {script name} .使用pm2 start npm --no-automation --name {app name} -- run {script name} It works.有用。 The --no-automation flag is there because without it PM2 will not restart your app when it crashes. --no-automation标志在那里,因为没有它 PM2 在崩溃时不会重新启动你的应用程序。

使用npm run

pm2 start npm --name "{app_name}" -- run {script_name}

I wrote shell script below (named start.sh ).我在下面编写了 shell 脚本(名为start.sh )。 Because my package.json has prestart option.因为我的package.jsonprestart选项。 So I want to run npm start .所以我想运行npm start

#!/bin/bash
cd /path/to/project
npm start

Then, start start.sh by pm2.然后,通过pm2启动start.sh

pm2 start start.sh --name appNameYouLike

I needed to run a specific npm script on my app in pm2 (for each env) In my case, it was when I created a staging/test service我需要在 pm2 中的应用程序上运行特定的 npm 脚本(对于每个 env)在我的情况下,它是在我创建临时/测试服务时

The command that worked for me (the args must be forwarded that way):对我有用的命令(必须以这种方式转发 args):

pm2 start npm --name "my-app-name" -- run "npm:script"

examples:例子:

pm2 start npm --name "myApp" -- run "start:test"

pm2 start npm --name "myApp" -- run "start:staging"

pm2 start npm --name "myApp" -- run "start:production"

Hope it helped希望有帮助

you need to provide app name here like myapp您需要在此处提供应用程序名称,例如 myapp

pm2 start npm --name {appName} -- run {script name}

you can check it by你可以通过

pm2 list

you can also add time你也可以添加时间

pm2 restart "id" --log-date-format 'DD-MM HH:mm:ss.SSS'

or或者

pm2 restart "id" --time

you can check logs by您可以通过以下方式检查日志

pm2 log "id"

or或者

pm2 log "appName"

to get logs for all app获取所有应用的日志

pm2 logs

是的,我们可以,现在 pm2 支持 npm start,--name 到物种应用程序名称。

pm2 start npm --name "app" -- start

See to enable clustering:请参阅启用集群:

pm2 start npm --name "AppName" -i 0 -- run start

What do you think?你怎么认为?

If you use PM2 via node modules instead of globally, you'll need to set interpreter: 'none' in order for the above solutions to work.如果您通过节点模块而不是全局使用 PM2,则需要设置interpreter: 'none'以使上述解决方案起作用。 Related docs here .相关文档在这里

In ecosystem.config.js :ecosystem.config.js

  apps: [
    {
      name: 'myApp',
      script: 'yarn',
      args: 'start',
      interpreter: 'none',
    },
  ],

pm2 start npm --name "custom_pm2_name" -- run prod

"scripts": {
    "prod": "nodemon --exec babel-node ./src/index.js"
  }

This worked for me when the others didnt当其他人没有时,这对我有用

Unfortunately no luck with running script start:prod . 不幸的是,运行脚本start:prod没有运气。

"scripts": {
   "start:prod": "node index.js"
},

Command pm2 start npm -- run start:prod crashes 命令pm2 start npm -- run start:prod崩溃

Don't forget the space before start开始前不要忘记空格

pm2 start npm --[space]start

so the correct command is:所以正确的命令是:

pm2 start npm -- start

Unfortunately, it seems that pm2 doesn't support the exact functionality you requested https://github.com/Unitech/PM2/issues/1317 .不幸的是,pm2 似乎不支持您请求的确切功能https://github.com/Unitech/PM2/issues/1317

The alternative proposed is to use a ecosystem.json file Getting started with deployment which could include setups for production and dev environments.建议的替代方案是使用ecosystem.json文件开始部署,其中可能包括生产和开发环境的设置。 However, this is still using npm start to bootstrap your app.但是,这仍然使用npm start来引导您的应用程序。

pm2 start ./bin/www

can running可以跑步

if you wanna multiple server deploy you can do that.如果你想部署多台服务器,你可以这样做。 instead of pm2 start npm -- start而不是 pm2 start npm -- start

It's working fine on CentOS 7它在 CentOS 7 上运行良好

PM2 version 4.2.1 PM2 版本 4.2.1

let's take two scenarios:让我们假设两个场景:

1. npm start //server.js 1. npm start //server.js

pm2 start "npm -- start" --name myMainFile

2. npm run main //main.js 2. npm run main //main.js

pm2 start "npm -- run main" --name myMainFile

To run PM2 with npm start method and to give it a name , run this,要使用npm start方法运行 PM2 并为其name ,请运行以下命令,
pm2 start npm --name "your_app_name" -- start

To run it by passing date-format for logs,要通过为日志传递日期格式来运行它,
pm2 start npm --name "your_name" --log-date-format 'DD-MM HH:mm:ss.SSS' -- start

For the normal user对于普通用户

PM2 now supports npm start: PM2 现在支持 npm start:

pm2 start npm -- start

To assign a name to the PM2 process, use the "--name" option:要为 PM2 进程分配名称,请使用“--name”选项:

pm2 start npm --name "your desired app name" -- start

For the root user对于 root 用户

sudo pm2 start npm -- start

To assign a name to the PM2 process, use the "--name" option:要为 PM2 进程分配名称,请使用“--name”选项:

sudo pm2 start npm --name "your desired app name" -- start

Now, You can use after:现在,您可以在之后使用:

pm2 start npm -- start

Follow by https://github.com/Unitech/pm2/issues/1317#issuecomment-220955319关注https://github.com/Unitech/pm2/issues/1317#issuecomment-220955319

Yes, Absolutely you can do it very efficiently by using a pm2 config (json) file with elegance.是的,绝对可以通过优雅地使用 pm2 配置(json)文件非常有效地完成它。

package.json file (containing below example scripts) package.json 文件(包含以下示例脚本)

"scripts": {
    "start": "concurrently npm:server npm:dev",
    "dev": "react-scripts start",
    "build": "node ./scripts/build.js",
    "eject": "react-scripts eject",
    "lint": "eslint src server",
    "shivkumarscript": "ts-node -T -P server/tsconfig.json server/index.ts"
  }

Suppose we want to run the script named as 'shivkumarscript' with pm2 utility.假设我们要使用 pm2 实用程序运行名为“shivkumarscript”的脚本。 So, our pm2 config file should be like below, containing 'script' key with value as 'npm' and 'args' key with value as 'run '.因此,我们的 pm2 配置文件应该如下所示,包含值为 'npm' 的 'script' 键和值为 'run' 的 'args' 键。 Script name is 'shivkumarscript' in our case.在我们的例子中,脚本名称是“shivkumarscript”。

ecosystem.config.json file生态系统.config.json 文件

module.exports = {
    apps: [
        {
            name: "NodeServer",
            script: "npm",
            automation: false,
            args: "run shivkumarscript",
            env: {
                NODE_ENV: "development"
            },
            env_production: {
                NODE_ENV: "production"
            }
        }
    ]
}

Assuming that you have already installed Node.js, NPM and PM2 on your machine.假设你已经在你的机器上安装了 Node.js、NPM 和 PM2。 Then below should be the command to start the application through pm2 which will in turn run the npm script (command line mentioned in your application's package.json file):然后下面应该是通过 pm2 启动应用程序的命令,它将依次运行 npm 脚本(在应用程序的 package.json 文件中提到的命令行):

For production environment:对于生产环境:

pm2 start ecosystem.config.js --env production --only NodeServer

For development environment:对于开发环境:

pm2 start ecosystem.config.js --only NodeServer

...And Boooom! ......还有Boooom! guys伙计们

You can change directory to your project您可以将目录更改为您的项目

cd /my-project

then run然后运行

pm2 start "npm run start" \\ run npm script from your package.json

read more here在这里阅读更多

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

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