I have Node.js app on AWS EC2 instance. This app consists out of 4 modules. To start my app from opened SSH terminal I use PM2and I run the following commands one by one:
pm2 start /opt/backend/app1/app1.js
pm2 start /opt/backend/app2/app2.js
pm2 start /opt/backend/app3/app3.js
pm2 start /opt/backend/app4/app4.js
It works perfect when I run the commands above from opened SSH terminal. However, when I make an image from the EC2 instance and I configure launch configuration to launch new instances from that image and I add the following bash script that should be run when new instance is created, new instances are being made and auto scaling works but Node.js app does not start:
#!/bin/bash
pm2 start /opt/backend/app1/app1.js
pm2 start /opt/backend/app2/app2.js
pm2 start /opt/backend/app3/app3.js
pm2 start /opt/backend/app4/app4.js
I tried the following as well, but it still does not work:
#!/bin/bash
pm2 start /home/username/opt/backend/app1/app1.js
pm2 start /home/username/opt/backend/app2/app2.js
pm2 start /home/username/opt/backend/app3/app3.js
pm2 start /home/username/opt/backend/app4/app4.js
Related
I want to set up ApplicationStart hook for node.js project where pm2 is used as a process manager in aws ec2 server.
I checked some tutorials and the shell script for ApplicationStart hook contains commands for running the project by using:
node/npm
pm2
for example in this tutorial, the shell script contains:
npm start
pm2 start npm --name "covidapp" -- start
in this tutorial, the shell script contains:
pm2 start npm --name "myApp"
node app.js > app.out.log 2> app.err.log < /dev/null &
Why we are running the project two times? Why we just don't use pm2?
I've deployed several apps on EC2 using PM2 and in my experience there should be no need (or benefit) to use node app.js, npm start or similar.
As you are probably already guessing, the whole point of PM2 is to run the process(es).
My recommendation would be to create a PM2 ecosystem configuration with all needed configurations, number of processes, ENV vars etc. I personally prefer this way even when running only one single node application on the server.
https://pm2.keymetrics.io/docs/usage/application-declaration/
And start the process(es) using the configuration, eg.:
pm2 start ecosystem.config.js
I also recommend using PM2 startup generator to make sure PM2 is started on server reboot: pm2 startup
https://pm2.keymetrics.io/docs/usage/startup/
Once you have the startup script generated. Start your processes using pm2 manually or by using a configuration file (see example above). Verify with pm2 status that all processes are running as expected and execute pm2 save to "snapshot" the current state. The saved state will now automatically respawn on reboot.
I am using pm2 for managing node processes on one of the servers.
The package is here: https://pm2.keymetrics.io/
It is open source and available both on npmjs and GitHub.
I can easily install it every time using: npm i pm2 -g
I love pm2, and not just node processes, I write bash scripts and run them as cron under pm2 and I can easily check the logs.
Some commands:
pm2 --name "process-name" start "bash script.sh"
pm2 --name "node-process" start "node main.js"
pm2 logs node-process
pm2 stop node-process
pm2 restart node-process
There are 2 more commands which are very useful to start pm2 on startup with all the processes automatically.
pm2 startup Will generate startup script.
pm2 save Will update start script with current processes.
Everything is good. But, today I got into a problem.
I am running all pm2 node processes from a folder /mnt/node.
What I want is that I have synced that /mnt/node folder to another server and I am trying to find a way to move all pm2 processes automatically to another server without writing each process once again.
May be someone can help.
You can do this.
On the source server:
pm2 save
copy file saved on ~/.pm2/dump.pm2 to destination server, then:
pm2 resurrect
Haven't try this between two differents server yet but i think it will be ok.
i have setup the node project on the AWS EC# instance.
npm start
is working. then project running on the 3000 port. but it will closing when close the terminal. then i have tried PM2.but when run using PM2 its not working. but status showing as online.
these are the command used to run PM2,
npm install pm2 -g
pm2 start app.js
pm2 startup
pm2 save
why is that ? how can i run it?
have you tried pm2 logs?
Also, you can set the project for default like whenever you want to run your server you can simply run by using the pm2 logs command, to set up your server here is the command
Go to your source folder or where your app.js file is stored, and then run this command
pm2 start npm --name "Your Project Name" -- run start
After setting up by this, you can always run your server with pm2 logs command
Thanks
hello I am total beginner when it comes to nodejs.
I was working on PHP code but found this app that will suit my needs for a project this is my first time to nodejs and managed to get this app working. https://github.com/KiraLT/torrent-stream-server .
the command to get it running is torrent-stream-server serve
which works great but i have to keep it running and not touch for it to work any help with this please?
i have spent my whole night searching i found like forever and pm2 but i did not know how to use since serve command
go to the folder path of project
npm i
npm install pm2#latest -g
pm2 start app_name if you want to run attached project to the question you can use this command: pm2 start npm --name "app_name" -- start
pm2 startup
pm2 save
note : pm2 startup can generate startup scripts and configure them in order to keep your process list intact across expected or unexpected machine restarts.
note : pm2 save to freeze a process list for automatic respawn
for managing application state is simple here are the commands :
pm2 ls
pm2 restart app_name
pm2 reload app_name
pm2 stop app_name
pm2 delete app_name
pm2 logs app_name
I am trying to use the module pm2 to start my node js app everytime the server boots.
I have used the command pm2 startup ubuntu but each time I restart the server, my application is not running and I have to start it manually again.
Any ideas what is causing this issue?
Make sure you do save your processes:
pm2 start app.js
pm2 startup ubuntu
pm2 save
Once you have started the apps and want to keep them on server reboot
do: pm2 save
Source: https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#startup-script
Go to your server directory and use the following commands:
pm2 start <your_app_name.js>
pm2 startup ubuntu
pm2 save