Node app crashes after closing the terminal, even using pm2 - node.js

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.

Related

Node.js app stops working as soon as the AWS Lightsail's Bitnami console is closed

I have developed a simple node.js application and I am trying to publish it on AWS Lightsail. I followed the instructions on this page: https://medium.com/#sharmasha2nk/aws-lightsail-bitnami-nodejs-letsencrypt-cf653573b8a1
I am connecting to Bitnami using the SHH console provided. After I start the app with node index.js command the app is up and running until I close the SHH console. As soon as I close it the application stops and “Service Unavailable” error is being displayed. How can I keep my app running on AWS even if my PC is shot down and the SHH console is closed?
I have tried restarting the apache server and AWS Lightsail instance. Neither of them helped.
httpd-app.conf file content:
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
httpd-prefix.conf file content:
Include "/opt/bitnami/apps/emsnotes/conf/httpd-app.conf"
Thanks.
I would recommend you pm2
Here's how to do it quickly:
install pm2 globally using sudo npm i -g pm2
navigate to the location of your index.js via ssh
run pm2 start index.js
This will keep node running even when you disconnect the ssh session or shutdown your development computer. The process will keep running on your server in the background. This is used for running node apps in production on a server.
You can read all about it here in its documentation.
I hope I could help you, please let me know if you have any question.
Karim
I had a look at the article and frankly i see nowhere that he talk about keeping the server up and running.
Basically he is using the Bitnami Node.js stack and if you look at the docs you will see that Bitnami use Forever.js to keep the apps running on their instance, Forever is a CLI tool to monitor and keep a Node instance running in the background.
Check the docs here : https://docs.bitnami.com/general/infrastructure/nodejs/administration/start-express-server/
Here is the Git for Forever, so can read more on it: https://github.com/foreverjs/forever

How to run node.js application on pm2

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

pm2 is not working in amazon ec2 with node application

I have node application hosted in amazon ec2.
I used nginx to point it in my domain.
It runs perfectly when i use npm start to run the application.
But when i use pm2, it doesnt work.
pm2 list command shows that the app is running.
But url doesn't work. Even when i again run npm start app gets started that clearly states that the port is free. So pm2 is not actually running anything on that port. But pm2 says app is online.
I was running the wrong file from pm2. As it was express project, so i had to run bin/www
So the following command solve the issue:
pm2 start bin/www
Answered it here as well:
https://github.com/Unitech/pm2/issues/3252
Thanks

how to deploy expressjs application on window server

There many tools to deploy expressjs application, such as strongloop,pm2.
But they do not work well on windows.
I have install [pm2]2 in my window computer, and start my app.
https://i.stack.imgur.com/ZpOIa.jpg
But when i log off my computer and log in again. the service is disappear.
So I install pm2-windows-service and install pm2 as a window service. It failed again.
Anyone have a good expressjs deployment plan?
You need to make a pm2 save when you started your app. PM2 need to know which apps were started and their state to restart them after a reboot, and thats the aim of the pm2 save command which will save the state of all apps in the pm2 directory.

Website stops working nodejs

I have website on Ubuntu sever.
I have started process using this command
sudo pm2 start app.js -i 0
it was working fine.Today morning my client message me it's stop working.
I have stoped the node and re-start the process again using same command.
Now I want to know the reason by this happens?
How i can do this to check the issue ?
Thanks
check pm2 logs folder ( PM2_PROCESS_USER_HOME_DIRECTORY/.pm2/logs)
May be server crash and you did't setup pm2 autostart
active pm2 autostart :
pm2 startup ubuntu
pm2 save

Resources