How to run node.js application on pm2 - node.js

I am new to pm2 manager and ssh. I developed one project in node.js and hosted into pm2 and ssh.That's Working fine and give correct output
Now my question is
Same way i hosted another project into pm2 server and then i follow below procedure to run this application.
Step1: ssh and Mycredentials
step2: cd folder projectname
step3 : pm2 start www
That shows Done.
I will run url i didn't get output default it shows first project ouput\
please help me!

Start your pm2 service with name argument to differentiate with multiple services
pm2 start www --name="my-sample-app1"
pm2 start index.js --name="my-sample-app2"
In order to list out the running process, enter the following command,
pm2 list
Using this you can debug the list of running process
To know the detailed information about your running process, use the following command
pm2 show my-sample-app1
pm2 show my-sample-app2

i tried but not working.
pm2 stop appname
pm2 delete appname
After enter above commands it show process is deleted,Then i enter second project and
pm2 start www
but i got first project output

Related

Node app crashes after closing the terminal, even using pm2

I'm doing web automation by using Puppeteer with NodeJS. It works fine when I'm logged in to the terminal. I'm using ec2 instance Ubuntu 22 server.
But when I'm exit out of the terminal and then try to perform the task it just loads and loads and do nothing. But when I log in to check the pm2 logs then it starts working again with the terminal open.
When I check pm2 logs I get this error every time.
Error in pm2 logs:
I suppose there is problem with the command you are trying to run the app. Use the following command to run the app via pm2.
npx pm2 start app
For Cpanel Users having this issue
Login to your WHM account, click on the terminal, cd into the directory where your node js application is, then pm2 start .This was how i solved my issue.

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 deploy a NodeJS app without root access, so it stays online after server restarts?

I'm trying to deploy my NodeJS app. I tried npm forever and pm2, but I believe that my hoster restarts the server every night because my app is always offline the next day.
The thing is, that I have a shared hosting server on A2hosting and don't have root access, so these sudo commands don't work here... except you know how I can change that, haha...
I don't know what infos I need to provide for you to help me... so if you need to know something, tell me please!
Thank you so much!
I've used pm2 to do that.
npm install -g pm2
then you run the following command to ensure pm2 will run on startup:
pm2 startup
It will show you the command you have to run.
Finally, you initiate the process:
pm2 start server.js --name <service-name>
Freeze your process list:
pm2 save
More details on pm2 documentation: https://github.com/Unitech/pm2

Resources