英文:
How to startup PM2 with multiple instances
问题
我正在使用PM2运行我的Node.js代码。为了我的项目,我需要多个运行相同代码的Node.js实例。因此,我创建了一个名为"myscript.sh"的脚本:
cd ~/myproject
PM2_HOME='.pm2_1' /usr/local/bin/node /usr/local/lib/node_modules/pm2/bin/pm2 start dist/app.js -- id=1 token=xxx isMaster=true
PM2_HOME='.pm2_2' /usr/local/bin/node /usr/local/lib/node_modules/pm2/bin/pm2 start dist/app.js -- id=1 token=yyy isMaster=false
# . . .
PM2_HOME='.pm2_x' /usr/local/bin/node /usr/local/lib/node_modules/pm2/bin/pm2 start dist/app.js -- id=1 token=zzz isMaster=false
当我通过CLI运行它时,它可以正常工作,例如bash myscript.sh
,但是通过crontab无法工作:
@reboot bash /home/user/myproject/myscript.sh
或者
* * * * * bash /home/user/myproject/myscript.sh
我还尝试使用pm2 startup
,但是它不支持PM2_HOME='..'
参数,无论我将PM2_HOME
设置为什么值,例如**.pm2_1**,它仍然会创建环境目录像/home/user/myproject/.pm2。似乎pm2 startup
只适用于单个实例。
所以我的问题是如何通过crontab运行myscript.sh?
我不知道是否相关,但我的代码是用TypeScript编写的。在本地主机上,我像这样运行它:
npm start -- id=1 token=zzz isMaster=false
其中在package.json中设置了"start":
"start": "npm run build && node dist/app"
"build": "rimraf dist && tsc"
请注意:
- 每个实例应该获取不同的参数。
- 解决方案应适用于启动情况,例如应用程序应在机器启动时启动。
英文:
I am running my nodejs code via PM2. For my project I need multiple instances of nodejs that running the same code. So I created a next script "myscript.sh":
cd ~/myproject
PM2_HOME='.pm2_1' /usr/local/bin/node /usr/local/lib/node_modules/pm2/bin/pm2 start dist/app.js -- id=1 token=xxx isMaster=true
PM2_HOME='.pm2_2' /usr/local/bin/node /usr/local/lib/node_modules/pm2/bin/pm2 start dist/app.js -- id=1 token=yyy isMaster=false
. . .
PM2_HOME='.pm2_x' /usr/local/bin/node /usr/local/lib/node_modules/pm2/bin/pm2 start dist/app.js -- id=1 token=zzz isMaster=false
It works when I run it via CLI like bash myscript.sh
, but it doesn't work via crontab:
@reboot bash /home/user/myproject/myscript.sh
or
* * * * * bash /home/user/myproject/myscript.sh
Also I tried to use pm2 startup
but it doesn't work with the PM2_HOME='..'
parameter, no matter what I set to PM2_HOME
, like .pm2_1, it still creates environment like /home/user/myproject/.pm2. Seems like pm2 startup
works for the single instances only.
So my question how can I run myscript.sh via crontab?
I don't know if it relevant my code is written in TS. On localhost I run it like this:
npm start -- id=1 token=zzz isMaster=false
where start is set in package.json:
"start": "npm run build && node dist/app"
"build": "rimraf dist && tsc"
Please notice that
- Every instance should get different parameters
- Solution should work for the startup case, like the app should startup with a machine start
答案1
得分: 3
为什么要使用 PM2_HOME 变量?PM2_HOME 变量在需要多个 PM2 守护程序实例时非常有用。
如果我理解正确,您需要运行多个脚本实例。这可以通过同一个 PM2 守护程序来完成!
要实现这一点,只需执行以下操作,假设您使用的是 Linux 操作系统。
1 - 使用不同的参数启动您相同脚本的所有实例:
pm2 -n "脚本 1" start /my/script.js -- param1=xxx param2=111
pm2 -n "脚本 2" start /my/script.js -- param1=yyy param2=222
pm2 -n "脚本 3" start /my/script.js -- param1=zzz param2=333
2 - 保存您的 PM2 配置:
pm2 save
3 - 启用 PM2 启动挂钩:
pm2 startup
4 - 完成!如果需要,您可以添加新的实例,只需键入 pm2 save
命令来注册新实例。
英文:
Why use PM2_HOME var? PM2_HOME var is usefull if you need multiple instance of PM2 daemon.
What you need, if i understand well, is running many instance of your script. This can be done with the same PM2 daemon!
To achieve this just do this, assuming you use linux box.
1 - Start all your instances of your same script with different parameters:
pm2 -n "Script 1" start /my/script.js -- param1=xxx param2=111
pm2 -n "Script 2" start /my/script.js -- param1=yyy param2=222
pm2 -n "Script 3" start /my/script.js -- param1=zzz param2=333
2 - Save your pm2 configuration:
pm2 save
3 - Enable pm2 startup hook:
pm2 startup
4 You are done! You can add new instance if needed, just type the pm2 save
command to register new instances.
答案2
得分: 1
要让PM2运行多个实例的Node应用程序,您需要使用集群模式。
查看文档:https://pm2.keymetrics.io/docs/usage/cluster-mode/
例如,您可以使用以下命令启动4个应用程序实例:
pm2 start app.js -i 4
--- 编辑 ---
要为每个进程提供不同的输入参数,我认为使用PM2没有简单的方法。
一个替代方法是创建一个新的JavaScript/TypeScript文件作为入口点。让该入口点创建多个子进程,每个子进程都是您的应用程序。然后,您可以为每个子进程传递不同的参数。
英文:
To have PM2 run multiple instances of your node app you'll want to use clustered mode.
See the docs here: https://pm2.keymetrics.io/docs/usage/cluster-mode/
For example, you can use this command to start 4 instances of your app:
pm2 start app.js -i 4
--- EDIT ---
For providing different input parameters per process I don't think there is any easy way to do that with PM2.
An alternative is to create a new JavaScript/TypeScript file as your entry point. Have that entry point create multiple child processes, each of which is your application. Then you could pass in different parameters for each.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论