有没有办法让 pm2 运行 npm start 脚本,还是只需要运行 pm2 start app.js
所以在开发中
npm start
然后在使用 pm2 进行生产时,您将运行类似
pm2 start 'npm start'
在 forever
中有一种等效的方法:
forever start -c "npm start" ./
PM2 现在支持 npm start:
pm2 start npm -- start
要为 PM2 进程分配名称,请使用 --name
选项:
pm2 start npm --name "app name" -- start
那些使用像 .json
文件这样的配置脚本来运行 pm2 进程的人可以使用 npm start
或任何其他类似的脚本 -
我的应用程序 pm2.json
{
"apps": [
{
"name": "my-app",
"script": "npm",
"args" : "start"
}
]
}
然后简单地 -
pm2 start my-app-pm2.json
编辑 - 当您在父目录中有此配置脚本并希望在子目录中启动应用程序时处理用例,然后使用 cwd
属性。
假设我们的应用程序位于相对于该配置文件的子目录 nested-app
中,那么 -
{
"apps": [
{
"name": "my-nested-app",
"cwd": "./nested-app",
"script": "npm",
"args": "start"
}
]
}
更多详细信息here。
start
脚本信息的 package.json 位于其中之一。请参阅此要点:gist.github.com/gianpaj/04c5680a8275616aac5e46374e07f673 当我运行它时,它不知道 npm
应该从哪里运行。谢谢
cwd
属性。检查我编辑的答案:)
sudo pm2 start config.json
身份运行 pm2 命令
使用 npm run
pm2 start npm --name "{app_name}" -- run {script_name}
是的。使用 pm2 start npm --no-automation --name {app name} -- run {script name}
。有用。 --no-automation
标志在那里 because without it PM2 will not restart your app when it crashes.
pm2 --help | grep 'no-autom'
在版本 1.0.2 中为我返回一行
我需要在 pm2 中的应用程序上运行特定的 npm 脚本(对于每个 env)在我的情况下,它是在我创建暂存/测试服务时
对我有用的命令(参数必须以这种方式转发):
pm2 start npm --name "my-app-name" -- run "npm:script"
例子:
pm2 start npm --name "myApp" -- run "start:test"
pm2 start npm --name "myApp" -- run "start:staging"
pm2 start npm --name "myApp" -- run "start:production"
希望它有所帮助
您需要在此处提供应用名称,例如 myapp
pm2 start npm --name {appName} -- run {script name}
你可以通过
pm2 list
您还可以添加时间
pm2 restart "id" --log-date-format 'DD-MM HH:mm:ss.SSS'
或者
pm2 restart "id" --time
您可以通过以下方式检查日志
pm2 log "id"
或者
pm2 log "appName"
获取所有应用程序的日志
pm2 logs
-- run {script name}
吗?我在 pm2 文档中找不到它。我已经阅读了您的答案的编辑历史,发现了这个有趣的编辑:in case of different script name use run with script name and in case of start script no need to add run
。如果我理解,使用 run {script name}
用于多个脚本,并且只有 npm start
使用 pm2 start npm --name {appName} -- start
更相关?另外,我不明白为什么 --
和 start/run
之间的空格有什么建议?谢谢
npm start
但如果是其他脚本,我们使用 run like npm run {script name}
来处理空间在 --
之间,我认为这只是他们使用的方式(不确定)如果您找到答案,请告诉我
我在下面编写了 shell 脚本(名为 start.sh
)。因为我的 package.json
有 prestart
选项。所以我想运行 npm start
。
#!/bin/bash
cd /path/to/project
npm start
然后,在 pm2 之前启动 start.sh
。
pm2 start start.sh --name appNameYouLike
startOrRestart
命令时不起作用。
是的,我们可以,现在 pm2 支持 npm start,--name 到物种应用程序名称。
pm2 start npm --name "app" -- start
请参阅启用集群:
pm2 start npm --name "AppName" -i 0 -- run start
你怎么看?
-i 0
做什么?
如果您通过节点模块而不是全局使用 PM2,则需要设置 interpreter: 'none'
才能使上述解决方案起作用。相关文档 here。
在 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"
}
当其他人没有时,这对我有用
对于普通用户
PM2 现在支持 npm start:
pm2 start npm -- start
要为 PM2 进程分配名称,请使用“--name”选项:
pm2 start npm --name "your desired app name" -- start
对于根用户
sudo pm2 start npm -- start
要为 PM2 进程分配名称,请使用“--name”选项:
sudo pm2 start npm --name "your desired app name" -- start
是的,您绝对可以通过优雅地使用 pm2 配置 (json) 文件来非常有效地做到这一点。
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"
}
假设我们要使用 pm2 实用程序运行名为“shivkumarscript”的脚本。因此,我们的 pm2 配置文件应该如下所示,包含值为 'npm' 的 'script' 键和值为 'run' 的 'args' 键。在我们的例子中,脚本名称是 'shivkumarscript'。
生态系统.config.json 文件
module.exports = {
apps: [
{
name: "NodeServer",
script: "npm",
automation: false,
args: "run shivkumarscript",
env: {
NODE_ENV: "development"
},
env_production: {
NODE_ENV: "production"
}
}
]
}
假设你已经在你的机器上安装了 Node.js、NPM 和 PM2。然后下面应该是通过 pm2 启动应用程序的命令,该命令将依次运行 npm 脚本(在应用程序的 package.json 文件中提到的命令行):
对于生产环境:
pm2 start ecosystem.config.js --env production --only NodeServer
对于开发环境:
pm2 start ecosystem.config.js --only NodeServer
......和Boooom!伙计们
您可以将目录更改为您的项目
cd /my-project
然后运行
pm2 start "npm run start" \\ run npm script from your package.json
阅读更多here
它在 CentOS 7 上运行良好
PM2 版本 4.2.1
让我们看两个场景:
1. npm start //server.js
pm2 start "npm -- start" --name myMainFile
2. npm run main //main.js
pm2 start "npm -- run main" --name myMainFile
不幸的是,pm2 似乎不支持您要求的确切功能 https://github.com/Unitech/PM2/issues/1317。
建议的替代方法是使用可以包含生产和开发环境设置的 ecosystem.json
文件 Getting started with deployment。但是,这仍然使用 npm start
来引导您的应用程序。
However, this is still using npm start to bootstrap your app.
您真的可以使用 pm2
运行 npm start
吗?
pm2 start ./bin/www
能跑
如果您想部署多个服务器,您可以这样做。而不是 pm2 start npm -- start
开始前不要忘记空格
pm2 start npm --[space]start
所以正确的命令是:
pm2 start npm -- start
要使用 npm start
方法运行 PM2 并为其提供 name
,请运行此命令,
pm2 start npm --name "your_app_name" -- start
要通过为日志传递日期格式来运行它,
pm2 start npm --name "your_name" --log-date-format 'DD-MM HH:mm:ss.SSS' -- start
现在,您可以在之后使用:
pm2 start npm -- start
关注https://github.com/Unitech/pm2/issues/1317#issuecomment-220955319
--name "app"
,但它只是将其设置为 npm。npm start
吗?"start": "cross-env NODE_ENV=production nodemon dist-prod/server.js"