puppeteer doesnt work in nohup mode but works normaly - node.js

Ive made a node js web scraper with puppeteer.
I want it to run 24/7 so I tried running it on a aws ec2 virtual machine.
if I run the program normally "npm start" it works perfectly, but beacause I want to close the ssh connection and go about my day, Ive tried running "nohup npm start &" but when I run it like that I either get an error in 'nohup.out':
UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.
or just the code stops running and nothing happens

ended up just using
ctrl + z
bg
disown -a
instead

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.

Npm start in background on a server after SSH connection (EC2)

I have an app that I start with "npm start"
I would like to run it in background.
This app is hosted on aws EC2.
I tried :
npm start &
Doesnt work if I disconnect ssh connection
nohup npm start &
Same, it fail if I disconnect ssh connection
I tried to launch with root, or user, doesnt make any difference.
I also tried forever but I didnt manage to launch with npm start like "forever start npm start" doesnt work
Thanks a lot for your help
You have to use docker.
You will need to read a lot the documentation, because it's complicated to understand if you dont know it : https://docs.docker.com/get-started/
But it does exactly what you want, running app in background in a container.
You can close everything your app will still run.
Enjoy reading !
I will try the docker style next time because im running out of time.
I found a way using :
"screen" command !
Actually the process stop after few hours with screen

I have hosted my Nodejs Application on Amazon AWS. Now it shows 502 bad gateway after some time

I started the server using Command prompt by the command "sudo node server.js"
Then I closed the command prompt.
Now after some time, the website automatically shows 502 bad gateway error.
I had also used the command "sudo node server.js &" but it still not works.
Can you help me find out what could be the possible problem.
Thanks in advance!
The problem is that you app is not running, here is why.
When start the application in the command line, the application is handled by that bash instance. When you close that instance, so does the application.
The solution to this is to use a package like pm2 which will make sure that your application is always running.

LocalHost and MongoDB

I am trying to run MongoDB using a Node express application.
It runs fine for the first time..but once i close and start the code again, I get an error saying that localhost 3000 is already in use.
Also, connecting mongoDB gives an error stating that localhost port 27017 does not accept request
When you run the command npm start it will run in background even after you close it using Ctrl + C. The process running in the background will block the port 3000, since it's being used.
Instead try running with the command node app.js. Change app.js for the main file of your server. (Restart the machine or kill the process before, so you can terminate the process created by your previous command npm start )
Another way to solve that problem is to manually kill the process started with the npm start command. npm start usually is used when the server is in production, since the command makes it run "forever".
Let me know if this solves your problem. :)

deploying nodejs and mongoose app in aws

I'm new to aws and recently I have been able to install node, mongod and also, FTPed project file to the server.
For mongodb,
I'm doing mongo in a separate terminal tab and starting service in another tab. I want to know how can I keep the mongo service running.
For node app,
Right now i'm doing node app in the server. How can I keep it alive too ?
Now the problem, is I open the browser with publicip:portno but nothing happens. How can I locate app and run it in the browser.
my app structure is
/
node
mongo
server.js and app related files
Using the Linux/Unix nohup command allows you to start commands that ignore the signals associated with the controlling terminal process terminating (SIGHUP). Adding the & to the command allows that command to run in the background and sending the output to /dev/null will ensure that your disk does not fill up with unnecessary log output. Here are some commands that should work:
nohup mongod >/dev/null &
node server.js >/dev/null &
This is more of a Linux/Unix command line issue I think. You can use the node module called forever to run your Node.js process in the background easily.
npm install -g forever
forever start YourScript.js
You can place an & at the end of the mongod command to place it in the background.
Ensure that in your node app you have the command app.listen("port number");
This is the "port number" that you should be using in your browser to render the page with the elastic IP from your AWS instance. Make sure that your elastic IP is configured to accept inbound requests.
To keep the service/app running in the background you can run the screen command then launch ur app/service (e.g. mongod OR node app.js). In the same terminal that your app is running press control + a + d, you should see
(detached)
printed on your screen.
This should keep your app/service running in the background.

Resources