node project run on AWS EC2 instance with PM2 - node.js

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

Related

Correct command for running node.js project with pm2 in ApplicationStart hook

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.

how to keep this app nodejs running even when closing ssh?

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

PM2 to start multiple Node Applications with npm start on boot(using raspberry pi)

How can I run multiple nodejs applications using command npm start in different directories using pm2 on boot? Also how do i setup them to start again if any error occurs?
I figured out how to start npm start with pm2 using - "pm2 start npm -- start"
but I don't understand how to set up them to boot.
Based on my research "pm2 startup" gives the command to setup startup which I passed as it is in the terminal.
How do I set up the rest?
Thanks,
If you've already got the part where pm2 automatically starts when you reboot, that's good.
You then need to add each of your apps to pm2.
Go into each of your project directories and run
pm2 start your_file.js --name=my_app, where your_file.js is the file you would use to start with regular node. (ie: node server.js, or node index.js, etc.) and my_app is a friendly name that you will see with pm2 status and can use to apply future commands to.
This should start your app. If you run pm2 status it is hopefully in the "running" state.
To save it so that it automatically gets booted when you reboot or restart pm2 you need to run pm2 save whenever you change app configurations.
You can do this for as many apps as you need to start.

How to run 2 separate apps in PM2?

Right now I am using pm2 to run a node server app. I do that with pm2 start npm. This seems to be independent of the current directory.
I found some mentions online to use pm2 start npm --name "app_name" -- start. However, no matter what name I specify and directory I am inside, it always starts the same app.
Due to the nature of node, I don't run a single .js file and just type npm start in the current directory.
Edit: From my understanding, the problem seems to be that pm2 always starts /usr/bin/npm (Starting /usr/bin/npm in fork_mode (1 instance). So the --name flag doesn't matter much, ie. I can get a list of the same app with different names, and this app is node app A and sometimes node app B. I am kinda lost
What is happening is you have a PM2 app named npm, thus the confusion. You can list pm2 apps with pm2 ls
First, remove it using :
pm2 del npm
Then, start a new app, naming it :
pm2 start npm --name "app_name" -- start
Then, the second app (in the other directory) with :
pm2 start npm --name "app_name2" -- start
You can run Multiple apps using PM2 just follow below steps:
Firstly enter into directory A and start it on PM2 pm2 start server.js --name app-name
Save this in PM2 using pm2 save
Now to run other app you need enter into directory B and start app using
pm2 start server.js --name app-name
Save this process as well, and now check PM2 list using pm2 ls

Starting node app whenever it stops

I don't have any errors in my node app, but it stops after some requests. Though i have enabled the logs and checked, its showing on different requests.
Is it possible to start the node app whenever it is stopped ?
Currently i am using this to start the node app
To start the app
nohup npm start &
And to find the process id
ps aux | grep node
And to kill certain process
kill -9 PID
I don't want to leave the app stopped. It should be ever running. So, Is it possible to start the app whenever it gets stopped.
Thanks
You can use PM2 to monitor and restart your app for you.
By default if you run your app with PM2 it will auto-restart it if it crashes.
npm install pm2#latest -g
pm2 start index.js
PM2 can also be used to run multiple instances and has commands to stop/restart individual ones.
You can get the process PID using pm2 list as well.
More robust use case:
Start up your app by having PM2 run 'npm start' and also give it a name to refer to it later (it will auto restart if it exits/crashes)
pm2 start npm --name="mySuperApp" -- start
Later you make some changes and want to restart it:
pm2 restart mySuperApp
Eventually you want to stop the app:
pm2 stop mySuperApp
you can use nodemon
installing it with npm install -g nodemon
and running your code like this : nodemon index.js
Finding more documentation here
It will run until your app encounter an error but you can restart it with rs in your console

Resources